Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's always tricky composing two-argument functions. Here's an implementation:</p> <pre><code>invert :: Ordering -&gt; Ordering invert GT = LT invert LT = GT invert EQ = EQ sosort :: (Ord a, Ord b) =&gt; [(a, b)] -&gt; [(a, b)] sosort = sortBy (\p p' -&gt; invert $ uncurry compare $ double fst p p') . sortBy (\p p' -&gt; uncurry compare $ double snd p p') where double f a a' = (f a, f a') </code></pre> <p>Because <code>sortBy</code> expects a function of two arguments, function composition isn't so nice.</p> <p>I have tested this code, and it works on your example.</p> <p>As Fred points out, you can write <code>compare EQ</code> instead of <code>invert</code>. As Dario points out, I could be using <code>on</code> from <code>Data.Function</code>, but in fact <code>on compare == comparing</code>, which I can use instead. Now the code can be read only by a Haskell Master:</p> <pre><code>sosort :: (Ord a, Ord b) =&gt; [(a, b)] -&gt; [(a, b)] sosort = sortBy (compare EQ `post` comparing fst) . sortBy (comparing snd) where post f g x x' = f (g x x') </code></pre> <p>I have compiled and run this code and it works on the original example.</p> <p>(I haven't got any votes for this answer, but thanks to good comments, I sure have learned a lot about the Haskell library. Who knows what function <code>post</code> is equivalent to? Not Hoogle...)</p> <p>It would be <strong>more idiomatic to write a suitable comparison function for pairs</strong>, but your question asked for consecutive sorts.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload