Stream: beginners

Topic: List.sortAsc / sortDesc docs


view this post on Zulip Alex Nuttall (Mar 02 2024 at 18:04):

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?

view this post on Zulip Brendan Hansknecht (Mar 02 2024 at 18:14):

I assume the doc is wrong and it is just supposed to sort lists of numbers.

view this post on Zulip Brendan Hansknecht (Mar 02 2024 at 18:16):

That said, sortby could be useful. Also, maybe ascending/descending should be a tag instead of in the function name.

view this post on Zulip Brendan Hansknecht (Mar 02 2024 at 18:16):

(deleted)

view this post on Zulip Brendan Hansknecht (Mar 02 2024 at 18:17):

Anyway, suggestions and api updates are definitely welcome.

view this post on Zulip Brendan Hansknecht (Mar 02 2024 at 18:19):

Oh also, there was definitely discussion on adding a proper Orderable or similar feature to make this generic.

view this post on Zulip Brendan Hansknecht (Mar 02 2024 at 18:20):

#ideas > ordering/sorting ability I think

view this post on Zulip Anton (Mar 02 2024 at 18:30):

I'll fix the docs.

view this post on Zulip Alex Nuttall (Mar 02 2024 at 18:53):

I do think think sortWith is pretty verbose, and sortBy covers 90% of the cases where it's useful

view this post on Zulip Brendan Hansknecht (Mar 02 2024 at 19:33):

Only for numbers, until we have something like ord, sortby is pretty limited

view this post on Zulip Richard Feldman (Mar 02 2024 at 19:51):

yeah nothing is blocking Ord if anyone wants to work on it!

view this post on Zulip Richard Feldman (Mar 02 2024 at 19:52):

we've talked about the design in https://roc.zulipchat.com/#narrow/stream/304641-ideas/topic/ordering.2Fsorting.20ability

view this post on Zulip Brendan Hansknecht (Mar 02 2024 at 21:22):

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?

view this post on Zulip Luke Boswell (Mar 04 2024 at 04:13):

I've just added an issue #6551 to track this.

view this post on Zulip Richard Feldman (Mar 04 2024 at 12:36):

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

view this post on Zulip Richard Feldman (Mar 04 2024 at 12:37):

(Rust doesn't have it for floats, seems to be ok)

view this post on Zulip Richard Feldman (Mar 04 2024 at 12:37):

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