The description in the docs:
Sorts a list in ascending order (lowest to highest), using a function which specifies a way to represent each element as a number.
makes me think of some other function which could be implemented in terms of List.sortWith
sortAscBy = \list, select ->
List.sortWith list \a, b ->
when (select a, select b) is
(sA, sB) if sA < sB -> LT
(sA, sB) if sA > sB -> GT
_ -> EQ
expect
actual =
sortAscBy
[
(1, "a"),
(3, "b"),
(2, "c"),
]
\(n, _) -> n
actual
== [(1, "a"), (2, "c"), (3, "b")]
rather than sortAsc itself which is not a higher order function. Is this a mistake?
I assume the doc is wrong and it is just supposed to sort lists of numbers.
That said, sortby could be useful. Also, maybe ascending/descending should be a tag instead of in the function name.
(deleted)
Anyway, suggestions and api updates are definitely welcome.
Oh also, there was definitely discussion on adding a proper Orderable
or similar feature to make this generic.
#ideas > ordering/sorting ability I think
I'll fix the docs.
I do think think sortWith is pretty verbose, and sortBy covers 90% of the cases where it's useful
Only for numbers, until we have something like ord, sortby is pretty limited
yeah nothing is blocking Ord
if anyone wants to work on it!
we've talked about the design in https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/ordering.2Fsorting.20ability
Wasn't sure if it resolved to s single design or not... Agreed implementation being this?
# Sort.roc
Sort implements
compare : a, a -> [LessThan, Equal, GreaterThan] where a implements Sort
Then need to add auto derive. Will not add it to string? But will add it to all numbers including floats?
I've just added an issue #6551 to track this.
Brendan Hansknecht said:
Will not add it to string? But will add it to all numbers including floats?
yeah, don't add it to Str
- and then I think let's start out with not adding it to floats and see how it goes in practice
(Rust doesn't have it for floats, seems to be ok)
maybe we'll end up wanting it, but it's easier to add it later than to try to remove it later :big_smile:
Last updated: Jul 26 2025 at 12:14 UTC