Home >Web Front-end >JS Tutorial >jquery sort array by index
Detailed explanation of JavaScript array sorting: Sort by index value and FAQ
This article will introduce how to efficiently sort arrays using JavaScript, especially the method of sorting by index values, and analyze their performance. In addition, some common questions about JavaScript array sorting will be answered.
An example of sorting by index value:
<code class="language-javascript">var data = Array(); data[0] = {"apples":1, "pears":2, "oranges":3}; data[1] = {"apples":3, "pears":3, "oranges":5}; data[2] = {"apples":4, "pears":1, "oranges":6}; console.log(data); //排序前 data.sort(function(a, b){ var a1= a.pears, b1= b.pears; if(a1== b1) return 0; return a1> b1? 1: -1; }); console.log(data); //排序后</code>
This code demonstrates how to sort an array based on the value of "pears". The results before and after sorting are clear at a glance: the number of pears is incremented from 1, 2, and 3.
<code class="language-javascript">// 对象数组 var array = [{id:'12', name:'Smith', value:1},{id:'13', name:'Jones', value:2}]; array.sort(function(a, b){ var a1= a.name, b1= b.name; if(a1== b1) return 0; return a1> b1? 1: -1; });</code>
<code class="language-javascript">// 数组数组 var array =[ ['12', 'Smith',1],['13', 'Jones',2]]; array.sort(function(a, b){ var a1= a[1], b1= b[1]; if(a1== b1) return 0; return a1> b1? 1: -1; });</code>
JavaScript Array Order FAQs (FAQs)
JavaScript's sort()
functions are sorted in ascending order by default. To sort in descending order, you need to pass in a comparison function. For example:
<code class="language-javascript">let array = [5, 2, 8, 1, 4]; array.sort(function(a, b) { return b - a; }); console.log(array); // 输出:[8, 5, 4, 2, 1]</code>
If b - a
returns a positive value, b
will be ranked before a
, achieving descending order.
can be achieved by passing in the comparison function to the sort()
function. The comparison function should return the difference between two objects' specific properties. For example:
<code class="language-javascript">let array = [ { name: 'John', age: 23 }, { name: 'Jane', age: 21 }, { name: 'Oliver', age: 25 } ]; array.sort(function(a, b) { return a.age - b.age; }); console.log(array);</code>
In this example, the array is sorted by the age
attribute of the object.
sort()
function to sort string arrays? Yes. The sort()
function is sorted by the Unicode value of the string by default, which may not match the expected order (for example, the Unicode value of uppercase letters is smaller than lowercase letters). If a case-insensitive sort is required, you can convert the string to lowercase or uppercase in the comparison function. For example:
<code class="language-javascript">let array = ['John', 'jane', 'Oliver']; array.sort(function(a, b) { return a.toLowerCase().localeCompare(b.toLowerCase()); }); console.log(array); // 输出:['jane', 'John', 'Oliver']</code>
sort()
function sorts numbers as strings by default, resulting in dictionary order. To sort numerical order, you need to pass in a comparison function to return the difference between the two numbers. For example:
<code class="language-javascript">let array = [10, 2, 30, 4]; array.sort(function(a, b) { return a - b; }); console.log(array); // 输出:[2, 4, 10, 30]</code>
sort()
function to sort the date array? Yes. The sort()
function can compare Date objects because they are converted to milliseconds since the Unix epoch when compared. For example:
<code class="language-javascript">let array = [ new Date(2021, 11, 1), new Date(2021, 10, 1), new Date(2021, 9, 1) ]; array.sort(function(a, b) { return a - b; }); console.log(array);</code>
In this example, the array is sorted in ascending order of dates.
The above is the detailed content of jquery sort array by index. For more information, please follow other related articles on the PHP Chinese website!