我有一个结构随机顺序列表:
L = [[{type1, Str12},{type2, Str22}, {code, Number12}, X],
[{type1, Str14},{type2, Str24}, {code, Number14}, X],
[{type1, Str15},{type2, Str25}, {code, Number15}, X],
[{type1, Str13},{type2, Str23}, {code, Number13}, X],
[{type1, Str11},{type2, Str21}, {code, Number11}, X],
...]
其中,StrX,NumberX是变量: 我必须对它进行排序,分别按第一个位置字符串,第二个位置字符串和第三个数字字符串排序。与列表中所有其他剩余元素无关。 例如。我们来说,
L1 = [{type1, "Hello"},{type2, "World"}, {code, "00009"}, "india", 24, ...],
L2 = [{type1, "Alarm"},{type2, "Started"}, {code, "00203"}, "Japan", -, ...],
...etc.
基本上L应该是这样的:
L = [[{type1, Str11},{type2, Str21}, {code, Number11}, X],
[{type1, Str12},{type2, Str22}, {code, Number12}, X],
[{type1, Str13},{type2, Str23}, {code, Number13}, X],
[{type1, Str14},{type2, Str24}, {code, Number14}, X],
[{type1, Str15},{type2, Str25}, {code, Number15}, X], ...].
对我来说很复杂但是,我尝试过这样但是没有用。
SortedHeirList = lists:sort(fun(A, B) ->
get_type1_val(A) =< get_type1_val(B),
get_type2_val(A) =< get_type2_val(B),
get_Code(A) =< get_code(B),
end, L).
答案 0 :(得分:1)
仅使用lists:sort/1
无效?
1> L1 = [{type1, "Hello"},{type2, "World"}, {code, "00009"}, "india", 24], L2 = [{type1, "Alarm"},{type2, "Started"}, {code, "00203"}, "Japan"].
[{type1,"Alarm"},{type2,"Started"},{code,"00203"},"Japan"]
2> lists:sort([L1, L2]).
[[{type1,"Alarm"},{type2,"Started"},{code,"00203"},"Japan"],
[{type1,"Hello"},{type2,"World"},{code,"00009"},"india",24]]
有关详细信息,请参阅8.11 Term Comparisons。