Python3 sorted() Function
Description
sorted() The function sorts all iterable objects.
Difference between sort and sorted:
sort is a method applied to list, while sorted can sort any iterable object.
The list sort method operates on an existing list, whereas the built-in sorted function returns a new list instead of modifying the original one.
Syntax
sorted syntax:
sorted(iterable, key=None, reverse=False)
Parameter Description:
- iterable -- an iterable object.
- key -- the element used for comparison; it takes a single argument, taken from the iterable, and specifies one element of the iterable to sort by.
- reverse -- sorting rule; reverse = True for descending, reverse = False for ascending (the default).
Return Value
Returns the reordered list.
Example
The following example shows the simplest use of sorted:
sorted
(
[
5
,
2
,
3
,
1
,
4
]
)
[
1
,
2
,
3
,
4
,
5
]
Ascending by default
You can also use the list.sort() method of list. This method modifies the original list (and returns None). It is usually less convenient than sorted() - if you do not need the original list, list.sort() is slightly more efficient.
a
=
[
5
,
2
,
3
,
1
,
4
]
a.
sort
(
)
a
[
1
,
2
,
3
,
4
,
5
]
Another difference is that the list.sort() method is defined only for list, while the sorted() function accepts any iterable.
sorted
(
{
1
:
'D'
,
2
:
'B'
,
3
:
'B'
,
4
:
'E'
,
5
:
'A'
}
)
[
1
,
2
,
3
,
4
,
5
]
Use key for reverse sorting
example_list
=
[
5
,
0
,
6
,
1
,
2
,
7
,
3
,
4
]
result_list
=
sorted
(
example_list
,
key
=
lambda
x: x*-
1
)
(
result_list
)
[
7
,
6
,
5
,
4
,
3
,
2
,
1
,
0
]
To sort in reverse order, pass the third parameter reverse=True:
example_list
=
[
5
,
0
,
6
,
1
,
2
,
7
,
3
,
4
]
sorted
(
example_list
,
reverse
=
True
)
[
7
,
6
,
5
,
4
,
3
,
2
,
1
,
0
]
Here is a more complex example that sorts a medal table; the data contains gold, silver and bronze counts for 15 countries, using \n as the separator:
Example
s
=
"Germany 10 11 16\nItaly 10 10 20\nNetherlands 10 12 14\nFrance 10 12 11\nUK 22 21 22\nChina 38 32 18\nJapan 27 14 17\nUSA 39 41 33\nROC 20 28 23\nAustralia 17 7 22\nHungary 6 7 7\nCanada 7 6 11\nCuba 7 3 5\nBrazil 7 6 8\nNew Zealand 7 6 7"
stodata
=
s.
split
(
'\n'
,
1
)
Use sorted
para
=
{
}
for
line
in
range
(
len
(
stodata
)
)
:
Each line of data
data
=
stodata
[
line
]
.
split
(
' '
)
(
data
)
Build the data structure para={'China': [], 'Russia': []}
para
[
data
[
0
]
]
=
[
int
(
'-'
- i
)
for
i
in
data
[
1
:
]
]
Start sorting (x[1] is the medal count, x[0] is the country)
new_para
=
sorted
(
para.
items
(
)
,
key
=
lambda
x:
(
x
[
1
]
,
x
[
0
]
)
)
(
)
c
=
[
]
for
i
in
new_para:
c.
append
(
(
i
[
0
]
)
)
for
j
in
range
(
15
)
:
(
f
"{(j+1):2d} {c[j]}"
)
The output is:
['德国', '10', '11', '16']
['意大利', '10', '10', '20']
['荷兰', '10', '12', '14']
['法国', '10', '12', '11']
['英国', '22', '21', '22']
['中国', '38', '32', '18']
['日本', '27', '14', '17']
['美国', '39', '41', '33']
['俄罗斯奥委会', '20', '28', '23']
['澳大利亚', '17', '7', '22']
['匈牙利', '6', '7', '7']
['加拿大', '7', '6', '11']
['古巴', '7', '3', '5']
['巴西', '7', '6', '8']
['新西兰', '7', '6', '7']
1 美国
2 中国
3 日本
4 英国
5 俄罗斯奥委会
6 澳大利亚
7 荷兰
8 法国
9 德国
10 意大利
11 加拿大
12 巴西
13 新西兰
14 古巴
15 匈牙利
Python3 built-in function