[lang-ref] ( sorted_by_col_index ) ( python )
def test_sorted_by_col_index():
# itemgetter
from operator import itemgetter
items = [
('Team-A', 974),
('Team-B', 962),
('Team-C', 985),
]
expected = [
('Team-B', 962),
('Team-A', 974),
('Team-C', 985),
]
r = sorted(items, key=itemgetter(1))
assert r == expected
def test_sorted_by_col_index_alternative1():
items = [
('Team-A', 974),
('Team-B', 962),
('Team-C', 985),
]
expected = [
('Team-B', 962),
('Team-A', 974),
('Team-C', 985),
]
r = sorted(items, key=lambda x: x[1])
assert r == expected
def test_sorted_by_col_index_alternative2():
# If key is omitted, tuples are compared lexicographically (by the first element, then next...).
items = [
('Team-C', 985),
('Team-B', 962),
('Team-A', 974),
]
expected = [
('Team-A', 974),
('Team-B', 962),
('Team-C', 985),
]
r = sorted(items)
assert r == expected