A question about https://github.com/roc-lang/roc/pull/6990, which implements the List.sortAscBy
and List.sortDescBy
builtins.
sortAscBy : List elem, (elem -> Num *) -> List elem
sortDescBy : List elem, (elem -> Num *) -> List elem
Sam raised a good question, and I'm not sure what the answer is. Looking for any ideas.
Is this really the right API? Since we have https://www.roc-lang.org/builtins/Num#compare, I think it would be better to use a
a, a -> [LT, GT, EQ]
comparator if possible.
Yeah, a, a -> [LT, GT, EQ]
seems better for consistency.
I think rust has both. sortWith
and sortWithField
I think. If we choose one, we definitely should go with a, a -> [LT, GT, EQ]
as it is more general and the other can always be defined in terms of it
So the question then is, do we still want the sortAscBy
and sortDescBy
then?
I'd be happy to close the PR.
I do like the clarity of sortAscBy
, you don't have to carefully inspect the function that sortWith
uses, you immediately know it's ascending.
Hmm, the original (elem -> Num *)
API may be better after all, it makes this case really easy:
testCompareFn : _ -> U8
testCompareFn = \n ->
when n is
ONE -> 1
TWO -> 2
THREE -> 3
FOUR -> 4
List.sortAscBy [THREE, ONE, TWO, FOUR] testCompareFn |> List.map testCompareFn
Oh, wait -- are we thinking the API should be more like sortAscBy : List elem, (a, a -> [LT, GT, EQ]) -> List elem
? I hadn't thought of that.
In that case, are we replacing List.sortWith
with these two?
Oh, wait -- are we thinking the API should be more like
That's how I understood it, what did you have in mind?
8 messages were moved here from #contributing > Casual Conversation by Luke Boswell.
In that case, are re replacing
List.sortWith
with these two?
Maybe, it would remove some doubt about which way you're sorting
For reference, this is what the original issue listed the API as.
sort : List elem, (elem, elem -> [ Lt, Eq, Gt ]) -> List elem
sortAsc : List (Num a) -> List (Num a)
sortDesc : List (Num a) -> List (Num a)
sortAscBy : List elem, (elem -> Num *) -> List elem
sortDescBy : List elem, (elem -> Num *) -> List elem
I would push for something like this:
sort: List elem -> List elem where elem implements Ordering
sortBy: List elem, (elem -> field) -> List elem where field implements Ordering
sortWith: List elem, (elem -> [ Lt, Eq, Gt ]) -> List elem
# Then the reversed versions:
sortReversed: List elem -> List elem where elem implements Ordering
sortByReversed: List elem, (elem -> field) -> List elem where field implements Ordering
sortWithReversed: List elem, (elem -> [ Lt, Eq, Gt ]) -> List elem
I could go with that
Just to clarify is (elem -> [ Lt, Eq, Gt ])
meant to be (elem, elem -> [ Lt, Eq, Gt ])
in the above?
And is Ordering
the same as Sortable PR?
Sort implements
compare : a, a -> [LessThan, Equal, GreaterThan] where a implements Sort
Yep and yep
Should match Sort
just wasn't sure what final naming we went with
Last updated: Jul 06 2025 at 12:14 UTC