| 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 |