我有一个 int 对列表,例如:
[(1,2),(3,5),(0,1),(1,3),(3,0),(0,3)]
我想从最小的对到最大的对这个列表进行排序。对于上面的示例,它应该是:
[(0,1),(0,3),(1,2),(1,3),(3,0),(3,5)].
您可以假设给定输入的列表中没有相同的对。我该怎么做?
Just use the function sort
from Data.List
. It works on any type which is an instance of Ord
and pairs are indeed instances.
Tuples of 2 Ord
values (including Int
s) are instances of Ord
class in turn, so you can just sort
the list.