SortByEmptyStringKeyMixedTest
`List.sortBy` orders by a String key that is `""` for some elements and `"x"` for others. `keyOf` returns `""` for even numbers and `"x"` for odd ones. Since `""` sorts before `"x"` and `List.sortBy` is stable, sorting `[ 1, 2, 3, 4 ]` gives the even numbers first, each group in its original order: `[2,4,1,3]`.
Results
elm-0.19.1
passran in 41 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.1/optimize for better performance and smaller assets. result: [2,4,1,3]
elm-0.19.2
passran in 74 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.2/optimize for better performance and smaller assets. result: [2,4,1,3]
elm-0.19.3
passran in 48 msOutput
Compiled in DEV mode. Follow the advice at https://elm-lang.org/0.19.3/optimize for better performance and smaller assets. result: [2,4,1,3]
lamdera-1.5.0
passran in 40 msOutput
result: [2,4,1,3]
Discussions
Discuss this test·Search the discussions on GitHub
Discussions are on GitHub; one about this test has [t:elm-core/SortByEmptyStringKeyMixedTest] in its title.
Source
The -- CHECK: lines give the expected output.
module SortByEmptyStringKeyMixedTest exposing (main)
{-| `List.sortBy` orders by a String key that is `""` for some elements and `"x"` for others.
`keyOf` returns `""` for even numbers and `"x"` for odd ones. Since `""` sorts
before `"x"` and `List.sortBy` is stable, sorting `[ 1, 2, 3, 4 ]` gives the even
numbers first, each group in its original order: `[2,4,1,3]`.
-}
-- CHECK: result: [2,4,1,3]
import Html exposing (text)
keyOf : Int -> String
keyOf n =
if modBy 2 n == 0 then
""
else
"x"
main =
let
result : List Int
result = List.sortBy keyOf [ 1, 2, 3, 4 ]
_ = Debug.log "result" result
in
text "done"