Question

I want to sort the elements of a Haskell list in lexicographic order, Is there any method?

Before sorted:

[ [], [1,2], [1], [1,2,3], [2], [1,3], [2,3], [3]]

After sorted:

[ [], [1], [1,2], [1,2,3], [1,3], [2], [2,3], [3]]
Was it helpful?

Solution

The default Ord instance for lists actually works that way:

Prelude> import Data.List
Prelude Data.List> sort [ [], [1,2], [1], [1,2,3], [2], [1,3], [2,3], [3]]
[[],[1],[1,2],[1,2,3],[1,3],[2],[2,3],[3]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top