Home  >  Article  >  Detailed explanation of sort function usage

Detailed explanation of sort function usage

zbt
zbtOriginal
2023-09-22 10:33:183866browse

The sort function is a sorting algorithm commonly used in programming. It is used to sort the elements in a sequence or list in a certain order. The usage of the sort function varies by programming language. The following introduces the usage of the sort function in several common programming languages:

The sort function in Python:

Python The sort function in is the built-in sorted function, which can sort lists or tuples. The usage of the sorted function is as follows:

sorted(iterable, key=None, reverse=False)

Parameter description:

iterable: the sequence or list that needs to be sorted.

key: Optional parameter, used to specify the sorting keyword. If the key parameter is not specified, the elements will be sorted by themselves by default.

reverse: Optional parameter, used to specify the sorting order. If reverse is True, the sorting result is in descending order; if reverse is False (default), the sorting result is in ascending order.

Example:

# 对列表进行升序排序
l = [3, 1, 4, 1, 5, 9]
sorted_l = sorted(l)
print(sorted_l) # 输出:[1, 1, 3, 4, 5, 9]
# 对列表进行降序排序
l = [3, 1, 4, 1, 5, 9]
sorted_l = sorted(l, reverse=True)
print(sorted_l) # 输出:[9, 5, 4, 3, 1, 1]
# 对元组进行排序,按照元组的第二个元素进行升序排序
t = [(1, 3), (1, 1), (2, 2), (3, 1)]
sorted_t = sorted(t, key=lambda x: x[1])
print(sorted_t) # 输出:[(1, 1), (1, 3), (3, 1), (2, 2)]

Sort function in JavaScript:

The sort function in JavaScript can sort an array. The usage of the sort function is as follows:

array.sort(compareFunction)

Parameter description:

array: the array to be sorted.

compareFunction: Optional parameter, used to specify the sorting rules. If the compareFunction parameter is not specified, it defaults to the Unicode string Code point order is sorted.

Example:

// 对数组进行升序排序
var arr = [5, 2, 8, 1, 3];
arr.sort();
console.log(arr); // 输出:[1, 2, 3, 5, 8]
// 对数组进行降序排序
var arr = [5, 2, 8, 1, 3];
arr.sort(function(a, b) {
return b - a;
});
console.log(arr); // 输出:[8, 5, 3, 2, 1]
// 对数组进行排序,按照数组的第二个元素进行升序排序
var arr = [(1, 3), (1, 1), (2, 2), (3, 1)];
arr.sort(function(a, b) {
return a[1] - b[1];
});
console.log(arr); // 输出:[(1, 1), (1, 3), (3, 1), (2, 2)]

The sort function in C:

The sort function in C can sort vectors. The usage of sort function is as follows:

#include
#include
// 对 vector 进行升序排序
std::vector vec = {5, 2, 8, 1, 3};
std::sort(vec.begin(), vec.end());
for (int i : vec) {
std::cout << i << &#39; &#39;;
} // 输出:1 2 3 5 8
// 对 vector 进行降序排序
std::vector vec = {5, 2, 8, 1, 3};
std

The above is the detailed content of Detailed explanation of sort function usage. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn