元组的列表,假设我有一个类似于下面的示例元组的列表:排序他们的第一个指数榆树
[(5, "a"), (1, "c"), (7, "d")]
在榆树
,我怎么去有关升序排序列表他们的第一个元素,以便我们得到以下结果?
[(1, "c"), (5, "a"), (7, "d")]
使用Elm List documentation,看来该sortBy
和sortWith
功能将是这种情况下非常有用。我在执行尝试如下:
maxTuples : Tuple(a, b) -> Tuple(a, b) -> Tuple(a, b)
maxTuples t1 t2 =
case compare t1 t2 of
((Tuple.first t1) >= (Tuple.first t2)) -> GT
_ -> LT
sortByFirst : List (Tuple (a, b)) -> List (Tuple (a, b))
sortByFirst lst =
List.sortWith maxTuples lst
不过,我得到以下性质编译器错误:
I ran into something unexpected when parsing your code!
99| ((Tuple.first t1) >= (Tuple.first t2)) -> GT
^
I am looking for one of the following things:
an upper case name
我的直觉是,编译器正在寻找GT
/LT
/EQ
每List
库的API,但如果这种情况下,我不知道我们将如何能够使用sortBy
或sortWith
排序在Elm的元组列表的每个元素的第一个索引。
编译器表明签名应该是 列表(可比,A2) - >列表(可比,A2) 但是这是个好主意。 –
@AdamFreymiller是的,这是更精确。我将把它编辑成答案。 –