sindresorhus/sort-on

Descending sort

sindresorhus opened this issue · 6 comments

Would be useful to be able to sort descending too.

I'm thinking a syntax like:

sortOn([{x: 'c', y: 'c'}, {x: 'b', y: 'a'}, {x: 'b', y: 'b'}], ['-x', 'y']);
// sorts `x` descending

I like it 👍

kevva commented

Yeah, currently I'm doing sortOn(arr, prop).reverse(), but would definitely be better to do it here.

@kevva Yup, but that won't work correctly with muliple sort properties with different sorting directions like the above example.

kevva commented

How would this look with functions and dot props? And what if you have a key named -- or whatever? I'd rather use opts.order/opts.sort/opts.desc or whatever to avoid edge cases.

How would this look with functions and dot props?

For dot props it would be the same -x.y. For functions I'm not really sure.

And what if you have a key named -- or whatever?

I've never seen that before, but as with dot-prop, we could allow escaping in that edge-case.

I'd rather use opts.order/opts.sort/opts.desc or whatever to avoid edge cases.

That would indeed be simpler, but would not enable different sorting between props as seen in my issue description.

kevva commented

I've never seen that before, but as with dot-prop, we could allow escaping in that edge-case.

Nah, me neither. Was just referring to that minimist thingy yesterday.

That would indeed be simpler, but would not enable different sorting between props as seen in my issue description.

Oh, that's a good point. Alright, I think this is worth a PR from me then :).