Proposal idea: descending sort

Temporal has a bunch of nice comparator functions like PlainDate.compare:

myDates.sort(PlainDate.compare);

So convenient!

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

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.

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!

2 Likes

This is pretty understandable:

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)

1 Like

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

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

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.

2 Likes