# Symbol.compareTo protocol

**URL:** <https://es.discourse.group/t/symbol-compareto-protocol/595>\
**Category:** 💡 Ideas\
**Created:** [December 24, 2020, 11:15am UTC](https://es.discourse.group/t/symbol-compareto-protocol/595 "2020-12-24T11:15:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![septs](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/septs/32/395_2.png) [@septs](https://es.discourse.group/u/septs)\
**Post date:** [December 24, 2020, 11:15am UTC](https://es.discourse.group/t/symbol-compareto-protocol/595/1 "2020-12-24T11:15:16Z")

</div>

```javascript
class Sample {
   constructor(value) {
      this.#internal = value
   }

   [Symbol.compareTo](b) { // returns `-1`, `0`, `1`
      if (input instanceof Sample) {
          return this.#internal < b.#internal ? 1 : -1
      }
      return -1
   }
}

const samples = [
    new Sample(2),
    new Sample(1),
]

samples.sort() // effect `Array.prototyp.sort` default behavior

```

References:

- [https://docs.microsoft.com/en-us/dotnet/api/system.icomparable.compareto?view=net-5.0](https://docs.microsoft.com/en-us/dotnet/api/system.icomparable.compareto?view=net-5.0)
- [https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Comparable.html](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/lang/Comparable.html)
- [https://tc39.es/ecma262/#sec-array.prototype.sort](https://tc39.es/ecma262/#sec-array.prototype.sort)

---

<div class="post-metadata">

**Author:** ![aclaymore](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/aclaymore/32/501_2.png) [@aclaymore](https://es.discourse.group/u/aclaymore)\
**Post date:** [December 24, 2020, 4:03pm UTC](https://es.discourse.group/t/symbol-compareto-protocol/595/2 "2020-12-24T16:03:47Z")

</div>

What would be the expected behavior when the array contains a mix of different types?

```javascript
class Sample1 {
   [Symbol.compareTo](b) {
     console.log('sample1 comparator called');
     return this.toString().localeCompare(b.toString());
   }
}

class Sample2 {
   [Symbol.compareTo](b) {
      console.log('sample2 comparator called');
      return this.toString().localeCompare(b.toString());
   }
}

const samples = [
    new Sample1(),
    new Sample2(),
]

samples.sort();
// sample1 called or sample2, or both?

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [December 24, 2020, 4:04pm UTC](https://es.discourse.group/t/symbol-compareto-protocol/595/3 "2020-12-24T16:04:33Z")

</div>

This concept was proposed this year, and rejected, here:  
[https://github.com/tc39/notes/blob/827c5f98554b6e04d81c2c265f78f6f7ddf8415a/meetings/2020-06/june-4.md#generic-comparison](https://github.com/tc39/notes/blob/827c5f98554b6e04d81c2c265f78f6f7ddf8415a/meetings/2020-06/june-4.md#generic-comparison)

---

<div class="post-metadata">

**Author:** ![septs](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/septs/32/395_2.png) [@septs](https://es.discourse.group/u/septs)\
**Post date:** [December 24, 2020, 4:15pm UTC](https://es.discourse.group/t/symbol-compareto-protocol/595/4 "2020-12-24T16:15:24Z")

</div>


