# Python 中 sort() 和 sorted() 的區別

Vaibhav Vaibhav 2023年1月30日

## Python 中的 `sort()` 函式

Python 中的 `sort` 函式通過交換索引處的值對列表進行原地排序。當我們對一個列表呼叫 `sort` 函式時，不會返回一個新列表；相反，相同的列表被排序。`sort()` 函式可用於對列表進行排序。

``````myList = [1, 5, 2, 10, 33, 3, 9]
print(myList)
myList.sort()
print(myList)
``````

``````[1, 5, 2, 10, 33, 3, 9]
[1, 2, 3, 5, 9, 10, 33]
``````

``````myList = [1, 5, 2, 10, 33, 3, 9]
print(myList)
myList.sort(reverse=True)
print(myList)
``````

``````[1, 5, 2, 10, 33, 3, 9]
[33, 10, 9, 5, 3, 2, 1]
``````

`sort` 函式還有另一個引數，即 `key`，可用於定義要考慮排序的值。例如，如果我們有一個對的列表，並且我們希望考慮第二個值對這些對進行排序，我們可以使用 `key` 引數來做到這一點。

``````def secondElement(element):
return element[1]

data = [(1, 2), (3, 4), (5, 3), (6, 6), (3, 0), (10, 1)]
print("Before sorting:", data)
data.sort(key=secondElement)
print("After sorting:", data)
``````

``````Before sorting: [(1, 2), (3, 4), (5, 3), (6, 6), (3, 0), (10, 1)]
After sorting: [(3, 0), (10, 1), (1, 2), (5, 3), (3, 4), (6, 6)]
``````

``````def byName(entry):
return entry["name"]

def byAge(entry):
return entry["age"]

def byRank(entry):
return entry["rank"]

data = [
{
"name": "Vaibhav",
"age": 18,
"rank": 1,
},
{
"name": "Steve",
"age": 24,
"rank": 5,
},
{
"name": "Olive",
"age": 12,
"rank": 2,
},
{
"name": "Allison",
"age": 17,
"rank": 4,
},
]

print("Original Data")
print(data)
print("Sorted by Name")
data.sort(key=byName)
print(data)
print("Sorted by Age")
data.sort(key=byAge)
print(data)
print("Sorted by Rank")
data.sort(key=byRank)
print(data)
``````

``````Original Data
[{'name': 'Vaibhav', 'age': 18, 'rank': 1}, {'name': 'Steve', 'age': 24, 'rank': 5}, {'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Allison', 'age': 17, 'rank': 4}]
Sorted by Name
[{'name': 'Allison', 'age': 17, 'rank': 4}, {'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Steve', 'age': 24, 'rank': 5}, {'name': 'Vaibhav', 'age': 18, 'rank': 1}]
Sorted by Age
[{'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Allison', 'age': 17, 'rank': 4}, {'name': 'Vaibhav', 'age': 18, 'rank': 1}, {'name': 'Steve', 'age': 24, 'rank': 5}]
Sorted by Rank
[{'name': 'Vaibhav', 'age': 18, 'rank': 1}, {'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Allison', 'age': 17, 'rank': 4}, {'name': 'Steve', 'age': 24, 'rank': 5}]
``````

## Python 中的 `sorted()` 函式

`sort` 函式一樣，`sorted` 也用於對列表進行排序。但是，`sorted` 函式返回一個新列表，而不是原地排序相同的列表。這是兩個函式之間的主要區別。讓我們通過一個例子來理解這一點。

``````myList = [1, 5, 2, 10, 33, 3, 9]
print("Before called sorted():", myList)
newList = sorted(myList)
print("After called sorted():", myList)
print("New list:", newList)
``````

``````Before called sorted(): [1, 5, 2, 10, 33, 3, 9]
After called sorted(): [1, 5, 2, 10, 33, 3, 9]
New list: [1, 2, 3, 5, 9, 10, 33]
``````

`reverse` 引數的示例。

``````myList = [1, 5, 2, 10, 33, 3, 9]
print("Before called sorted():", myList)
newList = sorted(myList, reverse=True)
print("After called sorted():", myList)
print("New list:", newList)
``````

``````Before called sorted(): [1, 5, 2, 10, 33, 3, 9]
After called sorted(): [1, 5, 2, 10, 33, 3, 9]
New list: [33, 10, 9, 5, 3, 2, 1]
``````

``````def byName(entry):
return entry["name"]

def byAge(entry):
return entry["age"]

def byRank(entry):
return entry["rank"]

data = [
{
"name": "Vaibhav",
"age": 18,
"rank": 1,
},
{
"name": "Steve",
"age": 24,
"rank": 5,
},
{
"name": "Olive",
"age": 12,
"rank": 2,
},
{
"name": "Allison",
"age": 17,
"rank": 4,
},
]

print("Original Data")
print(data)
print("Sorted by Name")
nameSorted = sorted(data, key=byName)
print(nameSorted)
print("Sorted by Age")
ageSorted = sorted(data, key=byAge)
print(ageSorted)
print("Sorted by Rank")
rankSorted = sorted(data, key=byRank)
print(rankSorted)
print("Data after all operations")
print(data)
``````

``````Original Data
[{'name': 'Vaibhav', 'age': 18, 'rank': 1}, {'name': 'Steve', 'age': 24, 'rank': 5}, {'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Allison', 'age': 17, 'rank': 4}]
Sorted by Name
[{'name': 'Allison', 'age': 17, 'rank': 4}, {'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Steve', 'age': 24, 'rank': 5}, {'name': 'Vaibhav', 'age': 18, 'rank': 1}]
Sorted by Age
[{'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Allison', 'age': 17, 'rank': 4}, {'name': 'Vaibhav', 'age': 18, 'rank': 1}, {'name': 'Steve', 'age': 24, 'rank': 5}]
Sorted by Rank
[{'name': 'Vaibhav', 'age': 18, 'rank': 1}, {'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Allison', 'age': 17, 'rank': 4}, {'name': 'Steve', 'age': 24, 'rank': 5}]
Data after all operations
[{'name': 'Vaibhav', 'age': 18, 'rank': 1}, {'name': 'Steve', 'age': 24, 'rank': 5}, {'name': 'Olive', 'age': 12, 'rank': 2}, {'name': 'Allison', 'age': 17, 'rank': 4}]
``````

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.