I have a list:
[[1,2],[1,2,3],[2,3],[4],[1,2,5,4],[2,3,1]]
I want to sort this list in ascending order and length with respect to sublists
like this:
[[4],[1,2],[2,3],[1,2,3],[2,3,1],[1,2,5,4]]
I found one solution, but that is not what I want
I found this function:
import Data.List
doubleSort :: (Ord a, Num a) => [[a]] -> [[a]]
doubleSort = sortOn sum . map sort
But this sorts the list in a way which I dont want:
[[1,2],[4],[2,3],[1,2,3],[1,2,3],[1,2,4,5]]
sort{[By,On]}
are stable sorts, so you can simply sort by the secondary critereon and then by the primary one.
> :m +Data.List
> sortOn length $ sort [[1,2],[1,2,3],[2,3],[4],[1,2,5,4],[2,3,1]]
[[4],[1,2],[2,3],[1,2,3],[2,3,1],[1,2,5,4]]
If it is already sorted lexicographically, then simply sortOn length
alone will do.
with one sort
> sortOn (length &&& sum)
&&&
is from Control.Arrow
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.