# More sort comparison functions

**URL:** <https://es.discourse.group/t/more-sort-comparison-functions/459>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [September 7, 2020, 9:42am UTC](https://es.discourse.group/t/more-sort-comparison-functions/459 "2020-09-07T09:42:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [September 7, 2020, 9:42am UTC](https://es.discourse.group/t/more-sort-comparison-functions/459/1 "2020-09-07T09:42:41Z")

</div>

[Temporal's going to have a `Temporal.Absolute.compare` function suitable for `Array.prototype.sort`.](https://github.com/tc39/proposal-temporal/blob/2ba1fa1971b4c07a6c398473231aac9b827b0de0/docs/absolute.md#temporalabsolutecompareone-temporalabsolute-two-temporalabsolute--number) By any chance, can we have some more comparison functions for existing data types, for similar uses?

- `Number.compare`
- `BigInt.compare`
- `String.compare` (useful when sorting an array of objects by a string key)
- `Date.compare`

Code for each of those would be pretty obvious:

```javascript
// Note: this considers `NaN`s as equal on purpose
Number.compare = (a, b) => {
    a = +a; b = +b
    if (a > b) return 1
    if (a < b) return -1
    return 0
}

BigInt.compare = (a, b) => {
    a = BigInt(a); b = BigInt(b)
    if (a > b) return 1
    if (a < b) return -1
    return 0
}

String.compare = (a, b) => {
    a = "" + a; b = "" + b
    if (a > b) return 1
    if (a < b) return -1
    return 0
}

Date.compare = (a, b) => {
    if (!(a instanceof Date && b instanceof Date)) throw new TypeError()
    return Number.compare(a.getTime(), b.getTime())
}

```

Also, each of those could be very easily optimized for by the engine, as they already have to do 99% of that just for a single `>` or `<` check, and they could just reduce it to effectively that kind of cost.

---

<div class="post-metadata">

**Author:** ![AshleyScirra](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ashleyscirra/32/125_2.png) [@AshleyScirra](https://es.discourse.group/u/AshleyScirra)\
**Post date:** [September 8, 2020, 1:20pm UTC](https://es.discourse.group/t/more-sort-comparison-functions/459/2 "2020-09-08T13:20:00Z")

</div>

This all looks handy and would help eliminate boilerplate code. How about a case-insensitive String.compare as well?

---

<div class="post-metadata">

**Author:** ![claudepache](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudepache/32/236_2.png) [@claudepache](https://es.discourse.group/u/claudepache)\
**Post date:** [September 14, 2020, 7:44am UTC](https://es.discourse.group/t/more-sort-comparison-functions/459/3 "2020-09-14T07:44:28Z")

</div>

> [@AshleyScirra](#):
>
> How about a case-insensitive String.compare as well

There is already `Intl.Collator` for that case.

---

<div class="post-metadata">

**Author:** ![claudiameadows](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/claudiameadows/32/126_2.png) [@claudiameadows](https://es.discourse.group/u/claudiameadows)\
**Post date:** [September 15, 2020, 7:31am UTC](https://es.discourse.group/t/more-sort-comparison-functions/459/4 "2020-09-15T07:31:24Z")

</div>

And to add onto that, `strings.sort((a, b) => a.localeCompare(b))` is equivalent to `strings.sort(new Intl.Collator().compare)`, just slightly less efficient.

Case-insensitive comparisons can't really be done in a language-independent manner, and my `String.compare` is just lexicographic and based on character code order. It's not actually useful for language in general, and as `.sort` does that by default, it's not useful there. However, it's useful for things like binary search of a sorted list of strings or other similar low-level locale-independent string tasks.
