元組的列表,假設我有一個類似於下面的示例元組的列表:排序他們的第一個指數榆樹
[(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是的,這是更精確。我將把它編輯成答案。 –