# Proposal idea: descending sort

**URL:** <https://es.discourse.group/t/proposal-idea-descending-sort/889>\
**Category:** 🦋 Proposals\
**Created:** [July 26, 2021, 9:02pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889 "2021-07-26T21:02:50Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![domenic](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/domenic/32/72_2.png) [@domenic](https://es.discourse.group/u/domenic)\
**Post date:** [July 26, 2021, 9:02pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/1 "2021-07-26T21:02:50Z")

</div>

Temporal has a bunch of nice comparator functions like [`PlainDate.compare`](https://tc39.es/proposal-temporal/docs/plaindate.html#compare):

```javascript
myDates.sort(PlainDate.compare);

```

So convenient!

But oh no, now I need to sort them in descending order instead of ascending:

```javascript
myDates.sort((a, b) => PlainDate.compare(b, a));

```

What is this madness?? Did someone make a typo switching the order? Did they forget that you don't need arrow function thunks when passing callbacks? No, they are just doing what they have been forced into by a cold, cruel world. Writing software has become joyless again, mere toil where we strive against the unfathomable machine to exhort our desired results from its CPUs.

But! This tragedy could be fixed by introducing a switch, e.g.

```javascript
myDates.sort(PlainDate.compare, { descending: true });
myDates.sortDescending(PlainDate.compare);

```

Ah yes, now _that_ is some code that is understandable in a single glance. Dare I say it, _that_ is art.

* * *

I don't have time to champion something like this myself, but if someone else wants to run with this, go for it!

---

<div class="post-metadata">

**Author:** ![lightmare](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lightmare/32/843_2.png) [@lightmare](https://es.discourse.group/u/lightmare)\
**Post date:** [July 26, 2021, 9:38pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/2 "2021-07-26T21:38:26Z")

</div>

This is pretty understandable:

```javascript
myDates.sort(PlainDate.compare).reverse();

```

Besides, the meaning of "reverse" is totally obvious regardless of what the comparison function does. "Descending" becomes a misnomer when the comparison function itself defines descending order, e.g. `myNames.sortDescending((a, b) => b.length - a.length)`

---

<div class="post-metadata">

**Author:** ![MaxGraey](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/maxgraey/32/651_2.png) [@MaxGraey](https://es.discourse.group/u/MaxGraey)\
**Post date:** [July 30, 2021, 3:04pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/3 "2021-07-30T15:04:27Z")

</div>

How about `arr.sortRight(PlainDate.compare)` which will be match with `reduceRight`?

Btw I proposed built-in `compare` class method for rest primitives:

> [@Builtin Ord / Compare method for primitives](https://es.discourse.group/t/builtin-ord-compare-method-for-primitives/724):
>
> Many languages contain special method for ordering which very useful for sorting or data structures with predicates like heap, priority queue, LRU cache and etc. Problem Sometimes implement ord or cmp / compare properly in user space is quite hard. Best example is numbers. Proper ordering (baseline): Float64Array.of(-1, -0, NaN, 1, -Infinity, +0, Infinity).sort() // \> [-Infinity, -1, -0, 0, 1, Infinity, NaN] But implement same ordering with ordinal Array's sort is quite difficult: [-1, -0, …

---

<div class="post-metadata">

**Author:** ![jamiebuilds](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/jamiebuilds/32/857_2.png) [@jamiebuilds](https://es.discourse.group/u/jamiebuilds)\
**Post date:** [August 11, 2021, 6:41pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/4 "2021-08-11T18:41:17Z")

</div>

The "right" in `reduceRight()` has more to do with the original order of the array and which direction you iterate over it. Where `sortDescending()` does not "iterate" over the items in a defined "order". It requests that the resulting array be in reverse order, however the sorting implementation wants to do that.

---

<div class="post-metadata">

**Author:** ![shaedrich](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/shaedrich/32/704_2.png) [@shaedrich](https://es.discourse.group/u/shaedrich)\
**Post date:** [December 3, 2024, 3:32pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/5 "2024-12-03T15:32:17Z")

</div>

> [@MaxGraey](#):
>
> How about `arr.sortRight(PlainDate.compare)` which will be match with `reduceRight`?

I suppose, `array.sortLast()` as in `findLast()` would not be better. But as we are talking of reversing, it could be along the lines of

- `array.sortReversed()`
- `array.sortInverse()`
- `array.sortTail()` (this might not be preferred due to this nomenclature not being used in the ecosystem yet to my knowledge)

---

<div class="post-metadata">

**Author:** ![bergus](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bergus/32/152_2.png) [@bergus](https://es.discourse.group/u/bergus)\
**Post date:** [December 4, 2024, 1:27am UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/6 "2024-12-04T01:27:18Z")

</div>

I would very much prefer not to add another `Array` method for this. Rather add a `Function` method!

```js
arr.sort(PlainDate.compare.flip())
arr.sort(PlainDate.compare.inverse())
arr.sort(Function.inverseComparison(PlainDate.compare))

```

Function composition is much more versatile.

---

<div class="post-metadata">

**Author:** ![bakkot](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/bakkot/32/22_2.png) [@bakkot](https://es.discourse.group/u/bakkot)\
**Post date:** [December 6, 2024, 6:43pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/7 "2024-12-06T18:43:58Z")

</div>

Yeah, a function to reverse a comparison function is much more natural.

That's what [Java](https://docs.oracle.com/javase/8/docs/api/java/util/Comparator.html#reversed--) and [Rust](https://doc.rust-lang.org/std/cmp/struct.Reverse.html) do, for example.

Only problem is where to put it. Function.prototype is kind of a weird place though I guess it would work. But I also think we should have some more built-in comparison functions, like the standard numeric one, so maybe a new namespace like `Compare`? As in, `array.sort(Compare.numeric)` for ascend sorting by numeric order, `array.sort(Compare.reverse(Compare.numeric))` for descending sort.

---

<div class="post-metadata">

**Author:** ![tabatkins](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tabatkins/32/170_2.png) [@tabatkins](https://es.discourse.group/u/tabatkins)\
**Post date:** [December 6, 2024, 11:12pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/8 "2024-12-06T23:12:26Z")

</div>

The number of times I've had to quickly write a test sort to remember whether I want `b-a` or `a-b`...

---

<div class="post-metadata">

**Author:** ![tabatkins](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/tabatkins/32/170_2.png) [@tabatkins](https://es.discourse.group/u/tabatkins)\
**Post date:** [December 6, 2024, 11:14pm UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/9 "2024-12-06T23:14:24Z")

</div>

A `Compare` namespace would be great for a few other things too, like being able to chain comparisons for tie-breaking, or compare arrays lexicographically, or use a key function on the comparator. I've written that code a few times; it's small and easy, but would be nice to have pre-supplied.

---

<div class="post-metadata">

**Author:** ![mhofman](https://avatars.discourse-cdn.com/v4/letter/m/f14d63/32.png) [@mhofman](https://es.discourse.group/u/mhofman)\
**Post date:** [December 10, 2024, 10:51am UTC](https://es.discourse.group/t/proposal-idea-descending-sort/889/10 "2024-12-10T10:51:55Z")

</div>

I've been wanting to propose a `String.compareByCodepoint` but I suppose a `Compare.stringByCodepoint` would work just as well.
