Home  >  Article  >  Web Front-end  >  How to use Js sort_javascript skills

How to use Js sort_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:00:461356browse

The parameter in array.sort() in javascript needs to be passed in a function, and it must be a function.

The x and y represent the two objects in the array.

It should be noted that there are only three return types: negative integer, zero, positive integer

It is generally customary to use -1 0 1 to represent

There is also a little trick , if you only need to compare one attribute, such as age here, then the entire sorting method is only one sentence

Copy code The code is as follows:

function a(x,y){
return x.age-y.age;
}
Statement:
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