Skip to content

Commit f548005

Browse files
committed
Merge pull request #1753 from pguyot/w29/add-lists-keysort-2
Add `lists:keysort/2` These changes are made under both the "Apache 2.0" and the "GNU Lesser General Public License 2.1 or later" license terms (dual license). SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
2 parents 43bed1d + 61d0d75 commit f548005

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [0.6.7] - Unreleased
88

9+
### Added
10+
11+
- Added `lists:keysort/2`
12+
913
### Fixed
1014

1115
- Fixed a bug where binary matching could fail due to a missing preservation of the matched binary.

libs/estdlib/src/lists.erl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
keyfind/3,
4444
keymember/3,
4545
keyreplace/4,
46+
keysort/2,
4647
keystore/4,
4748
keytake/3,
4849
foldl/3,
@@ -330,6 +331,17 @@ keyreplace(Key, N, [H | Tail], _OrigL, NewTuple, Acc) when element(N, H) =:= Key
330331
keyreplace(Key, N, [H | Tail], OrigL, NewTuple, Acc) ->
331332
keyreplace(Key, N, Tail, OrigL, NewTuple, [H | Acc]).
332333

334+
%%-----------------------------------------------------------------------------
335+
%% @param N the position in the tuple to compare (1..tuple_size)
336+
%% @param L the list to sort
337+
%% @returns The list L sorted by Nth element
338+
%% @doc Sort a list of tuples by Nth element.
339+
%% @end
340+
%%-----------------------------------------------------------------------------
341+
-spec keysort(N :: pos_integer(), L :: [tuple()]) -> [tuple()].
342+
keysort(N, TupleList) ->
343+
sort(fun(E1, E2) -> element(N, E1) < element(N, E2) end, TupleList).
344+
333345
%%-----------------------------------------------------------------------------
334346
%% @param Key the key to match
335347
%% @param N the position in the tuple to compare (1..tuple_size)

tests/libs/estdlib/test_lists.erl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ test() ->
3232
ok = test_keyfind(),
3333
ok = test_keydelete(),
3434
ok = test_keyreplace(),
35+
ok = test_keysort(),
3536
ok = test_keystore(),
3637
ok = test_keytake(),
3738
ok = test_foldl(),
@@ -120,6 +121,18 @@ test_keyreplace() ->
120121
]),
121122
ok.
122123

124+
test_keysort() ->
125+
?ASSERT_MATCH(lists:keysort(1, [{2, foo}, {1, bar}]), [{1, bar}, {2, foo}]),
126+
?ASSERT_MATCH(lists:keysort(1, [{2, foobar}, {1, foo}, {1, bar}]), [
127+
{1, foo}, {1, bar}, {2, foobar}
128+
]),
129+
?ASSERT_MATCH(lists:keysort(1, [{2, foo, zot}, {1, bar}]), [{1, bar}, {2, foo, zot}]),
130+
?ASSERT_MATCH(lists:keysort(1, []), []),
131+
?ASSERT_MATCH(lists:keysort(2, [{foo, 2}, {bar, 1}]), [{bar, 1}, {foo, 2}]),
132+
?ASSERT_ERROR(lists:keysort(1, [1, 2])),
133+
?ASSERT_ERROR(lists:keysort(3, [{1, bar}, {2, foo}])),
134+
ok.
135+
123136
test_keystore() ->
124137
?ASSERT_MATCH(lists:keystore(a, 1, [], {foo, bar}), [{foo, bar}]),
125138
?ASSERT_MATCH(lists:keystore(a, 1, [{a, x}, b, []], {1, 2}), [{1, 2}, b, []]),

0 commit comments

Comments
 (0)