Skip to main content
Additional explanations.
Source Link
E. Chan-López
  • 31.3k
  • 3
  • 29
  • 50
l1 = {{"c", 3.5}, {"a", 2.2}, {"d", 1.2}, {"b", 0.5}};

l2 = {{"a", 1}, {"b", 3}, {"c", 5}, {"d", 9}};

Using the third argument of GroupBy:

f = Values[
Sort@GroupBy[Join[#1, #2], First, 
  DeleteDuplicates@*Join @@ 
    ReverseSortBy[#, MatchQ[#[[2]], _Integer] &] &]] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

Or using GroupBy and SortBy:

f = Values@GroupBy[Sort@Join[#1, #2], 
    First, {#1[[1]], Splice@SortBy[#2, ! IntegerQ[#] &]} & @@ Thread@# &] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

Or using Association:

sortPair = SortBy[#, ! IntegerQ[#] &] &@*Identity;
assoc = Partition[Sort[Association@*Rule @@@ Join @@ #], Length@#] &;

Merge[assoc@{l1, l2}, sortPair]

(*<|"a" -> {1, 2.2}, "b" -> {3, 0.5}, "c" -> {5, 3.5}, "d" -> {9, 1.2}|>*)

KeyValueMap[{#1, Splice@#2} &]@%

(*{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}*)
l1 = {{"c", 3.5}, {"a", 2.2}, {"d", 1.2}, {"b", 0.5}};

l2 = {{"a", 1}, {"b", 3}, {"c", 5}, {"d", 9}};

Using the third argument of GroupBy:

f = Values[
Sort@GroupBy[Join[#1, #2], First, 
  DeleteDuplicates@*Join @@ 
    ReverseSortBy[#, MatchQ[#[[2]], _Integer] &] &]] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

Or using GroupBy and SortBy:

f = Values@GroupBy[Sort@Join[#1, #2], 
    First, {#1[[1]], Splice@SortBy[#2, ! IntegerQ[#] &]} & @@ Thread@# &] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

l1 = {{"c", 3.5}, {"a", 2.2}, {"d", 1.2}, {"b", 0.5}};

l2 = {{"a", 1}, {"b", 3}, {"c", 5}, {"d", 9}};

Using the third argument of GroupBy:

f = Values[
Sort@GroupBy[Join[#1, #2], First, 
  DeleteDuplicates@*Join @@ 
    ReverseSortBy[#, MatchQ[#[[2]], _Integer] &] &]] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

Or using GroupBy and SortBy:

f = Values@GroupBy[Sort@Join[#1, #2], 
    First, {#1[[1]], Splice@SortBy[#2, ! IntegerQ[#] &]} & @@ Thread@# &] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

Or using Association:

sortPair = SortBy[#, ! IntegerQ[#] &] &@*Identity;
assoc = Partition[Sort[Association@*Rule @@@ Join @@ #], Length@#] &;

Merge[assoc@{l1, l2}, sortPair]

(*<|"a" -> {1, 2.2}, "b" -> {3, 0.5}, "c" -> {5, 3.5}, "d" -> {9, 1.2}|>*)

KeyValueMap[{#1, Splice@#2} &]@%

(*{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}*)
Additional explanations.
Source Link
E. Chan-López
  • 31.3k
  • 3
  • 29
  • 50
l1 = {{"c", 3.5}, {"a", 2.2}, {"d", 1.2}, {"b", 0.5}};

l2 = {{"a", 1}, {"b", 3}, {"c", 5}, {"d", 9}};

Using the third argument of GroupBy:

f = Values[
Sort@GroupBy[Join[#1, #2], First, 
  DeleteDuplicates@*Join @@ 
    ReverseSortBy[#, MatchQ[#[[2]], _Integer] &] &]] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

Or using GroupBy and SortBy:

f = Values@GroupBy[Sort@Join[#1, #2], 
    First, {#1[[1]], Splice@SortBy[#2, ! IntegerQ[#] &]} & @@ Thread@# &] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

l1 = {{"c", 3.5}, {"a", 2.2}, {"d", 1.2}, {"b", 0.5}};

l2 = {{"a", 1}, {"b", 3}, {"c", 5}, {"d", 9}};

Using the third argument of GroupBy:

f = Values[
Sort@GroupBy[Join[#1, #2], First, 
  DeleteDuplicates@*Join @@ 
    ReverseSortBy[#, MatchQ[#[[2]], _Integer] &] &]] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

l1 = {{"c", 3.5}, {"a", 2.2}, {"d", 1.2}, {"b", 0.5}};

l2 = {{"a", 1}, {"b", 3}, {"c", 5}, {"d", 9}};

Using the third argument of GroupBy:

f = Values[
Sort@GroupBy[Join[#1, #2], First, 
  DeleteDuplicates@*Join @@ 
    ReverseSortBy[#, MatchQ[#[[2]], _Integer] &] &]] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

Or using GroupBy and SortBy:

f = Values@GroupBy[Sort@Join[#1, #2], 
    First, {#1[[1]], Splice@SortBy[#2, ! IntegerQ[#] &]} & @@ Thread@# &] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}

Source Link
E. Chan-López
  • 31.3k
  • 3
  • 29
  • 50

l1 = {{"c", 3.5}, {"a", 2.2}, {"d", 1.2}, {"b", 0.5}};

l2 = {{"a", 1}, {"b", 3}, {"c", 5}, {"d", 9}};

Using the third argument of GroupBy:

f = Values[
Sort@GroupBy[Join[#1, #2], First, 
  DeleteDuplicates@*Join @@ 
    ReverseSortBy[#, MatchQ[#[[2]], _Integer] &] &]] &;

f[l1, l2]

{{"a", 1, 2.2}, {"b", 3, 0.5}, {"c", 5, 3.5}, {"d", 9, 1.2}}