On the difference between the application of Sort() method in Python and Javascript

   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.

insert image description here



   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.

insert image description here



   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.

insert image description here



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)


insert image description here



   In Javascript, the sorting of arrays can also be achieved with the sort() method.

insert image description here



   Another example, when there are 2 or more digits in the array, then sort() will have a bug.

insert image description here



   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:



insert image description here



var list = [81, 132, 24, 51, 1, 2];
      list.sort(function (a, b) {
    
    
        return a - b;
      });
      console.log(list);


   descending order:



insert image description here



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.



insert image description here



   Output in ascending order:



insert image description here



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



insert image description here



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:



insert image description here



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:



insert image description here



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));
   

Guess you like

Origin blog.csdn.net/weixin_48591974/article/details/130094283