I mixed up the input to make sure the result is stable for arbitrary input order:
(sort #(or (> (last %1) (last %2)) (and (= (last %1) (last %2)) (< (first %1) (first %2)))) (shuffle '((1 2) (2 4) (3 2) (4 10) (5 3) (6 1) (7 2)))) => ((4 10) (2 4) (5 3) (1 2) (3 2) (7 2) (6 1))
Here we check, even with 1000 reordering the input, there is only one output order:
(count (into #{} (repeatedly 1000 (fn [] (sort #(or (> (last %1) (last %2)) (and (= (last %1) (last %2)) (< (first %1) (first %2)))) (shuffle '((1 2) (2 4) (3 2) (4 10) (5 3) (6 1) (7 2)))))))) => 1
Here we show that a large number of orderings are made in random order:
(count (into #{} (repeatedly 1000 (fn [] (shuffle '((1 2) (2 4) (3 2) (4 10) (5 3) (6 1) (7 2))))))) => 899
(of course, since the random case of random sampling will vary, the result will usually be in the range of 850-950).
source share