Home > Web Front-end > JS Tutorial > Detailed explanation of Javascript array sorting_Basic knowledge

Detailed explanation of Javascript array sorting_Basic knowledge

WBOY
Release: 2016-05-16 16:33:11
Original
1198 people have browsed it

If you have been working with JavaScript for a while, you must know the array sorting function sort. Sort is a method in the array prototype, namely array.prototype.sort(), sort(compareFunction), where compareFunction is a comparison function. Let’s take a look at a description from Mozilla MDN:
If compareFunction is not supplied, elements are sorted by converting them to strings and comparing strings in lexicographic (“dictionary” or “telephone book,” not numerical) order. For example, “80″ comes before “9″ in lexicographic order, but in a numeric sort 9 comes before 80.

Look at some simple examples below:

Copy code The code is as follows:

// Output [1, 2, 3]
console.log([3, 2, 1].sort());

// Output ["a", "b", "c"]
console.log(["c", "b", "a"].sort());

// Output [1, 2, "a", "b"]
console.log(["b", 2, "a", 1].sort());


As you can see from the above example, the default is to sort by alphabetical order in the dictionary.

Fortunately, sort accepts a custom comparison function, as in the following example:

Copy code The code is as follows:

function compareFunction(a, b) {
if( a > b) {
Return -1;
}else if(a < b) {
Return 1;
}else {
return 0;
}
}
//Outputs ["zuojj", "Benjamin", "1"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction));

After sorting, we have another question, how to control ascending and descending order?

Copy code The code is as follows:

function compareFunction(flag) {
flag = flag ? flag : "asc";
return function(a, b) {
if( a > b) {
Return flag === "desc" ? -1 : 1;
}else if(a < b) {
Return flag === "desc" ? 1 : -1;
}else {
Return 0;
}
};
}
//Outputs ["1", "Benjamin", "zuojj"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction()));
//Outputs ["zuojj", "Benjamin", "1"]
console.log(["Benjamin", "1", "zuojj"].sort(compareFunction("desc")));

The sorting rules of comparFunction are as follows:
1.If it returns a negative number, a will be sorted to a lower index in the array.
2.If it returns a positive number, a will be sorted to a higher index.
3.And if it returns 0 no sorting is necessary.

Let’s take a look at an excerpt from Mozilla MDN:
The behavior of the sort method changed between JavaScript 1.1 and JavaScript 1.2.To explain this description, let’s look at an example:

In JavaScript 1.1, on some platforms, the sort method does not work. This method works on all platforms for JavaScript 1.2.

In JavaScript 1.2, this method no longer converts undefined elements to null; instead it sorts them to the high end of the array. Please click here for details.

Copy code The code is as follows:

var arr = [];
arr[0] = "Ant";
arr[5] = "Zebra";
//Outputs ["Ant", 5: "Zebra"]
console.log(arr);
//Outputs 6
console.log(arr.length);
//Outputs "Ant*****Zebra"
console.log(arr.join("*"));
//Sort
var sortArr = arr.sort();
//Outputs ["Ant", "Zebra"]
console.log(sortArr);
//Outputs 6
console.log(sortArr.length);
//Outputs "Ant*Zebra****"
console.log(sortArr.join("*"));

I hope this article will be helpful for you to learn and understand the sort() method. I hope you will criticize and correct any inappropriateness in the article.

Reference link: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template