[英]Haskell function which sorts a list of integer lists according to the length of the sublists
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. sort{[By,On]}
是稳定的排序,因此您可以简单地按次要标准排序,然后按主要标准排序。
> :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.如果它已经按字典顺序排序,那么仅
sortOn length
就可以了。
with one sort
有一个
sort
> sortOn (length &&& sum)
&&&
is from Control.Arrow &&&
来自 Control.Arrow
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.