Stream: beginners

Topic: Sorting


view this post on Zulip Joshua Warner (Dec 24 2022 at 07:02):

I'm trying to sort a list of something that boils down to strings, and running into some issues. First, I see that there's nothing like List.sortByKey .name (to access the name field and sort by that as a key) - and nor do things like List seem to be comparable. When I try to encode everything in strings and write my own comparison function to use with List.sortWith, like so:

            set
                |> Set.toList
                |> List.sortWith \{modu: ma, name: na}, {modu: mb, name: nb} ->
                    a = Str.concat (Str.joinWith (List.map ma \m -> Str.concat m "::") "") na
                    b = Str.concat (Str.joinWith (List.map mb \m -> Str.concat m "::") "") nb
                    if a == b then
                        EQ
                    else
                        if a < b then
                            LT
                        else
                            GT

I just get:

── TYPE MISMATCH ───────────────────────── crates/compiler/parse/generator.roc ─

This 1st argument to isLt has an unexpected type:

48│                      else if a < b then
                                 ^

This a value is a:

    Str

But isLt needs its 1st argument to be:

    Num a

... which is very surprising. Is string comparison not implemented in Roc yet?

view this post on Zulip Richard Feldman (Dec 24 2022 at 07:27):

yeah we have Hash and beEq but not Ord yet

view this post on Zulip Richard Feldman (Dec 24 2022 at 07:28):

implementing it should be very similar to Hash I think!


Last updated: Jul 05 2025 at 12:14 UTC