core icon indicating copy to clipboard operation
core copied to clipboard

List.sortBy produces different order between Chromium/Chrome and Firefox

Open wknapik opened this issue 7 years ago • 4 comments

Edit: @evancz moved this gist directly into the issue.

2 6 3 4 5 1 7 10 9 11 8  -- Chromium/Chrome 66.0.3359.139 and 66.0.3359.170
2 6 1 3 4 5 7 9 10 11 8  -- Firefox 59.0.2 and 60.0

Tested on Linux.

import Html exposing (div, text)
import List
import Tuple

main =
   Html.beginnerProgram { model = [], view = view, update = (\_ -> \x -> x) }

view _ =
   div [] <| List.map (div [] << List.singleton << text << Tuple.first) (List.sortBy Tuple.second tuples)

tuples =
   [ ( "1", "foo" )
   , ( "2", "bar" )
   , ( "3", "foo" )
   , ( "4", "foo" )
   , ( "5", "foo" )
   , ( "6", "baz" )
   , ( "7", "foo" )
   , ( "8", "quux" )
   , ( "9", "foo" )
   , ( "10", "foo" )
   , ( "11", "foo" )
   ]

Thanks to @showell for helping out with this on slack.

wknapik avatar May 13 '18 17:05 wknapik

Behavior unchanged as of Chromium 66.0.3359.170 and Firefox 60.0.

wknapik avatar May 14 '18 13:05 wknapik

I am running into the same issue with Node 10 vs 11 (running elm-test on Travis). For Chrome it seems to be fixed since v70: https://mobile.twitter.com/mathias/status/1036626116654637057?s=19. The latest Chrome indeed shows the correct result for me (like Firefox).

eriktim avatar May 19 '19 21:05 eriktim

To add some background: Elm's List.sortBy uses Array.prototype.sort under the hood. That sorting function is not guaranteed to be stable, i.e. the original order is not guaranteed for equally sorted values.

eriktim avatar May 19 '19 21:05 eriktim

As of EC2020, Array.prototype.sort must be stable. See here: https://tc39.github.io/ecma262/#sec-array.prototype.sort

So this will come to all evergreen JS environments in time.

robinheghan avatar May 20 '19 06:05 robinheghan