-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add key and reverse parameters to builtin sorted function
Implement:bazelbuild/starlark#20 (comment) Closes #8881. PiperOrigin-RevId: 258401453
- Loading branch information
1 parent
3c00063
commit cc2c4ee
Showing
3 changed files
with
126 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
assert_eq(sorted([42, 123, 3]), [3, 42, 123]) | ||
assert_eq(sorted(["wiz", "foo", "bar"]), ["bar", "foo", "wiz"]) | ||
assert_eq(sorted([42, 123, 3], reverse=True), [123, 42, 3]) | ||
assert_eq(sorted(["wiz", "foo", "bar"], reverse=True), ["wiz", "foo", "bar"]) | ||
assert_eq(sorted(list({"a": 1, "b": 2})), ['a', 'b']) | ||
--- | ||
|
||
def f(x): | ||
return x[0] | ||
pairs = [(4, 0), (3, 1), (4, 2), (2, 3), (3, 4), (1, 5), (2, 6), (3, 7)] | ||
|
||
assert_eq(sorted(pairs, key=f), | ||
[(1, 5), | ||
(2, 3), (2, 6), | ||
(3, 1), (3, 4), (3, 7), | ||
(4, 0), (4, 2)]) | ||
|
||
--- | ||
assert_eq(sorted(["two", "three", "four"], key=len), | ||
["two", "four", "three"]) | ||
assert_eq(sorted(["two", "three", "four"], key=len, reverse=True), | ||
["three", "four", "two"]) | ||
assert_eq(sorted([[1, 5], [0, 10], [4]], key=max), | ||
[[4], [1, 5], [0, 10]]) | ||
assert_eq(sorted([[1, 5], [0, 10], [4]], key=min, reverse=True), | ||
[[4], [1, 5], [0, 10]]) | ||
assert_eq(sorted([[2, 6, 1], [5, 2, 1], [1, 4, 2]], key=sorted), | ||
[[1, 4, 2], [5, 2, 1], [2, 6, 1]]) | ||
|
||
--- | ||
sorted(1) ### type 'int' is not a collection | ||
--- | ||
sorted([1, 2, None, 3]) ### Cannot compare NoneType with int | ||
--- | ||
sorted([1, "one"]) ### Cannot compare string with int | ||
--- | ||
sorted([1, 2, 3], key=1) ### "int" object is not callable |