> Erlang中文手册 > keymerge/3 合并两个元组列表并排序

lists:keymerge/3

合并两个元组列表并排序

用法:

keymerge(N, TupleList1, TupleList2) -> TupleList3

内部实现:

-spec keymerge(N, TupleList1, TupleList2) -> TupleList3 when
      N :: pos_integer(),
      TupleList1 :: [T1],
      TupleList2 :: [T2],
      TupleList3 :: [(T1 | T2)],
      T1 :: Tuple,
      T2 :: Tuple,
      Tuple :: tuple().

keymerge(Index, T1, L2) when is_integer(Index), Index > 0 -> 
    case L2 of
	[] ->
	    T1;
	[H2 | T2] ->
	    E2 = element(Index, H2),
	    M = keymerge2_1(Index, T1, E2, H2, T2, []),
	    lists:reverse(M, [])
    end.

合并 2 个元组列表,合并好的新元组列表按元组的第 N 个值进行排序

TupleList1 = [{a, 1}, {d, 4}],
TupleList2 = [{b, 2}, {c, 3}],
lists:keymerge(2, TupleList1, TupleList2).