Home > Web Front-end > JS Tutorial > Introduction to JSON numerical sorting and multi-field sorting_javascript skills

Introduction to JSON numerical sorting and multi-field sorting_javascript skills

WBOY
Release: 2016-05-16 17:22:22
Original
1256 people have browsed it
Copy code The code is as follows:

//Sort array
function SortBy(field, reverse, primer ) {
reverse = (reverse) ? -1 : 1;
return function (a, b) {
a = a[field];
b = b[field];
if (typeof (primer) != 'undefined') {
a = primer(a);
b = primer(b);
}
if (a < b) return reverse * -1;
if (a > b) return reverse * 1;
return 0;
}
}
nodes.sort(SortBy('orderNum', false, parseInt)) ;

Merge the fields into a single one and then sort the strings
Copy the code The code is as follows:

nodes.sort(function (a, b) { return a.sort.localeCompare(b.sort) });
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