>>> sortOn fst [(2, "world"), (4, "!"), (1, "Hello")] [(1,"Hello"),(2,"world"),(4,"!")]
>>> sortOn length ["jim", "creed", "pam", "michael", "dwight", "kevin"] ["jim","pam","creed","kevin","dwight","michael"]
>>> sortBy (comparing fst) [(3, 1), (2, 2), (1, 3)] [(1,3),(2,2),(3,1)]Or, for the exact same API as sortOn, you can use `sortBy . comparing`:
>>> (sortBy . comparing) fst [(3, 1), (2, 2), (1, 3)] [(1,3),(2,2),(3,1)]
>>> sortOn fst $ (2, "world") :| [(4, "!"), (1, "Hello")] (1,"Hello") :| [(2,"world"),(4,"!")]
>>> sortOn List.length ("jim" :| ["creed", "pam", "michael", "dwight", "kevin"]) "jim" :| ["pam","creed","kevin","dwight","michael"]
>>> sortBy (comparing fst) $ (3, 1) :| [(2, 2), (1, 3)] (1,3) :| [(2,2),(3,1)]Or, for the exact same API as sortOn, you can use `sortBy . comparing`:
>>> (sortBy . comparing) fst $ (3, 1) :| [(2, 2), (1, 3)] (1,3) :| [(2,2),(3,1)]sortWith is an alias for `sortBy . comparing`.
sortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]If, instead, sortBy had been used, length would be evaluated on every comparison, giving <math> evaluations, rather than <math>. If f is very cheap (for example a record selector, or fst), sortBy (compare `on` f) will be faster than sortOn f.
>>> sortOn fst $ (2, "world") :| [(4, "!"), (1, "Hello")] (1,"Hello") :| [(2,"world"),(4,"!")]
>>> sortOn length $ "jim" :| ["creed", "pam", "michael", "dwight", "kevin"] "jim" :| ["pam","creed","kevin","dwight","michael"]
>>> sortBy (comparing fst) $ (3, 1) :| [(2, 2), (1, 3)] (1,3) :| [(2,2),(3,1)]Or, for the exact same API as sortOn, you can use `sortBy . comparing`:
>>> (sortBy . comparing) fst $ (3, 1) :| [(2, 2), (1, 3)] (1,3) :| [(2,2),(3,1)]sortWith is an alias for `sortBy . comparing`. Since: 4.20.0.0
>>> sortOn fst [(2, "world"), (4, "!"), (1, "Hello")] [(1,"Hello"),(2,"world"),(4,"!")]
>>> sortOn length ["jim", "creed", "pam", "michael", "dwight", "kevin"] ["jim","pam","creed","kevin","dwight","michael"]
>>> sortBy (comparing fst) [(3, 1), (2, 2), (1, 3)] [(1,3),(2,2),(3,1)]Or, for the exact same API as sortOn, you can use `sortBy . comparing`:
>>> (sortBy . comparing) fst [(3, 1), (2, 2), (1, 3)] [(1,3),(2,2),(3,1)]@since base-4.8.0.0
>>> sortOn fst [(2, "world"), (4, "!"), (1, "Hello")] [(1,"Hello"),(2,"world"),(4,"!")]
>>> sortOn fst [(2, "world"), (4, "!"), (1, "Hello")] [(1,"Hello"),(2,"world"),(4,"!")]
>>> sortOn id (select [1,4,2,3,3,7]) [1,2,3,3,4,7]
>>> sortOn fst [(2, "world"), (4, "!"), (1, "Hello")] [(1,"Hello"),(2,"world"),(4,"!")]The argument must be finite.
sortOn length (fromList ("alligator" :| ["monkey", "zebra"])) == fromList ("zebra" :| ["monkey", "alligator"])If, instead, sortBy had been used, length would be evaluated on every comparison, giving <math> evaluations, rather than <math>. If f is very cheap (for example a record selector, or fst), sortBy (compare `on` f) will be faster than sortOn f.
>>> sortOn fst [(2, "world"), (4, "!"), (1, "Hello")] [(1,"Hello"),(2,"world"),(4,"!")]
>>> sortOn length ["jim", "creed", "pam", "michael", "dwight", "kevin"] ["jim","pam","creed","kevin","dwight","michael"]
>>> sortOn fst $ slist [(2, "world"), (4, "!"), (1, "Hello")] Slist {sList = [(1,"Hello"),(2,"world"),(4,"!")], sSize = Size 3}Note: this function hangs on infinite slists.
sortOn negate (mk6 1 4 2 8 5 7) === mk6 8 7 5 4 2 1
unstableSortOn length (fromList ["alligator", "monkey", "zebra"]) == fromList ["zebra", "monkey", "alligator"]If, instead, unstableSortBy had been used, length would be evaluated on every comparison, giving <math> evaluations, rather than <math>. If f is very cheap (for example a record selector, or fst), unstableSortBy (compare `on` f) will be faster than unstableSortOn f.