Stream: ideas

Topic: ✔ List.sortBy


view this post on Zulip Ryan Bates (Dec 08 2023 at 17:38):

I would find a List.sortBy function convenient. It would sort by the result of a function (or record property). You could use it like this.

input
|> Str.split "\n"
|> List.map createPerson
|> List.sortBy .age

It could be implemented like this.

sortBy = \list, func ->
    List.sortWith list \a, b ->
        Num.compare (func a) (func b)

I'm not sure about sorting descending, perhaps it needs sortAscBy and sortDescBy?

view this post on Zulip LoipesMas (Dec 08 2023 at 17:41):

List.sortAscBy and List.sortDescBy are listed as todo in #664

view this post on Zulip Ryan Bates (Dec 08 2023 at 17:45):

Glad to see it's planned, thanks!

view this post on Zulip Notification Bot (Dec 08 2023 at 17:45):

Ryan Bates has marked this topic as resolved.


Last updated: Jun 16 2026 at 16:19 UTC