This article describes the usage of sort function sort in JS. Share it for your reference, as follows:
I recently encountered an interview question about sorting. In order to improve my knowledge points, I will write a study note here.
<html><head><TITLE>class_obj_js_class</TITLE><script language=javaScript>//sort() method is sorted by ASCII code size by default. See the following two examples function sortDemo(){ var a, l; // Declare variables. a = new Array("X" ,"y" ,"d", "Z", "v","m","r"); l = a.sort(); // Sort array. alert(l); return(l); // Returns the sorted array. }//sortDemo();function sortDemo2(){ var arr = [50,200,3000]; arr.sort(); alert(arr);}//The displayed results are 200, 3000, 50, because they are judged based on the first letters 2, 3, 5 //sortDemo2();//Sort the above numbers according to the upper case. If condition is a judgment of size, so even if the sorting of size function testFunction(){ var numArr = new Array(9, 23, 124); numArr.sort(function compare(a, b){ if(a>b){ return 1;//Sort the above numbers according to the upper case, if the condition is a judgment of size, so even if the sorting of size function testFunction(){ var numArr = new Array(9, 23, 124); numArr.sort(function compare(a, b){ if(a>b){ return 1;//Sort in ascending order, that is, a is after b} else return -1; }); alert(numArr);}//testFunction();//If the value returned by sort(sortfunction) in a>b is true, it is arranged in descending order, otherwise it is arranged in ascending order function testFunction2(){ var numArr = new Array(9,124,23); numArr.sort(function compare(a,b){ alert("a : " + a); alert("b : " + b); if(a > b){ alert("a > b, return positive number"); return -1;//Sort in descending order, that is, a is ahead of b} else{ alert("a < b, return negative number"); return 1; } }); alert(numArr);}//testFunction2();/*To make the elements of the array be arranged in order of odd numbers first and even numbers, if a and b need to be exchanged, only if a and b are even numbers satisfied, then sort from small to large numbers. Only when a and b are both odd numbers or even numbers and a>b can be seen from the following example that according to the conditions of if, the requirements for sorting are required, the return value is greater than 0, which is the sorting of the condition once*/function testAdvance(){ var a = [1, 7, 3, 9, 5, 6, 2, 8, 4]; function f(a, b) { if (0 == a % 2 && 1 == b % 2) { return 1;//Sort even and odd numbers} if ((1 == a % 2 && 1 == b % 2 || 0 == a % 2 && 0 == b % 2) && a > b) { return 1;//Sort odd and even numbers} return -1; } alert(a.sort(f));}//testAdvance();//What we need to do is write out the conditions in if, this condition is to return the conditions that a and b need to be exchanged//For example: var a=["a","A","B","b"]; and sort from large to small, only if a.toString().toLowerCase() < When b.toString().toLowerCase(), a and b are exchanged, so use this to fill the if condition. function testAdvance1(){ var a=["a","A","B","b"]; a.sort(function test(a,b){ if(a.toString().toLowerCase() < b.toString().toLowerCase()){ return 1; } else return -1; }); alert(a);}testAdvance1();</script><body >For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.