The Sort() method is believed to be familiar to everyone. It is used for sorting, that is, numbers from low to high (default) and from high to low. The following is a detailed example of the difference between the application of this sorting method in Python and Java Script.
In Python, for example, there is lst1: [1,5,8,6,2], and the list should be sorted from small to large.
It can be seen that the output result is [1,2,5,6,8], which meets the requirement of sorting from small to large.
As another example, there is a list lst2: [3,5,88,66,100,22]
It can be seen that the output result is [3, 5, 22, 66, 88, 100], which also meets the requirement of sorting from small to large.
In addition, we can also output the list in descending order. It can be realized by adding the parameter reverse = True. For the specific parameter method, you can refer to the official document. The default is ascending, and the parameter defaults to False.
lst1 = [1,5,8,6,2]
lst2 = [3,5,88,66,100,22]
lst1.sort(reverse=True)
lst2.sort(reverse=True)
print(lst1)
print(lst2)
In Javascript, the sorting of arrays can also be achieved with the sort() method.
Another example, when there are 2 or more digits in the array, then sort() will have a bug.
It can be seen that the output results are not output in ascending order. This is also the difference from the above Python. We can implement ascending and descending order through Javascript's fixed encapsulation method.
ascending order:
var list = [81, 132, 24, 51, 1, 2];
list.sort(function (a, b) {
return a - b;
});
console.log(list);
descending order:
var list = [81, 132, 24, 51, 1, 2];
list.sort(function (a, b) {
return b - a;
});
console.log(list);
It can be seen that this self-problem can be solved by encapsulating a function function(a,b).
Next, I will explain in detail how to use the native writing method to implement sorting in addition to the implementation of the Sort() method.
Suppose there is num_list = [32,22,35,11,49,30], the numbers in the list (array) need to be sorted in ascending order from small to large.
The following is the process of sorting logic. Sorting this array requires 3 rounds of comparison, and each round also has a corresponding number of comparisons. The maximum number of comparison rounds is: array length - 1, and the maximum number of comparisons is: array length - 1 - comparison rounds. Therefore, we can achieve it through nested loops, the parent loop controls the number of comparison rounds, and the sub-loop controls the number of comparisons. Then in the sub-loop, add a comparison condition, and when it is met, perform the exchange between the two values, so as to achieve the sorted result.
Output in ascending order:
num_list = [32,22,35,11,49,30]
def sort_lst(lst):
for i in range(len(lst) -1 ):
for j in range(len(lst) - 1 - i):
if lst[j] > lst[j+1]:
temp = lst[j+1]
lst[j+1] = lst[j]
lst[j] = temp
return lst
print(sort_lst(num_list))
Similarly, descending sorting can also be achieved
num_list = [32,22,35,11,49,30]
def sort_lst(lst):
for i in range(len(lst) -1 ):
for j in range(len(lst) - 1 - i):
if lst[j] < lst[j+1]:
temp = lst[j+1]
lst[j+1] = lst[j]
lst[j] = temp
return lst
print(sort_lst(num_list))
The same native logic can also be written in Javascript:
ascending order:
var array = [32,22,35,11,49,30];
function sort(array) {
for (var i = 0; i < array.length - 1; i++) {
for (var j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
var temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array;
}
console.log(sort(array));
descending order:
var array = [32,22,35,11,49,30];
function sort(array) {
for (var i = 0; i < array.length - 1; i++) {
for (var j = 0; j < array.length - 1 - i; j++) {
if (array[j] < array[j + 1]) {
var temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
return array;
}
console.log(sort(array));