Home > Web Front-end > JS Tutorial > body text

JavaScript object array sorted in descending order by a certain property

巴扎黑
Release: 2016-11-25 10:11:05
Original
2118 people have browsed it





<script> <br>//Define an object array <br>  <br>var data = [{ <br>name: "Overseas Business Department", <br>value: 0.58 <br>}, { <br>name: "domestic sales", <br>value: 0.36 <br>}, { <br>name: "Internet Center", <br>value: 0.78 <br>}]; <br>//Define a comparator--descending order <br>function compare( propertyName) { <br>return function(object1, object2) { <br>var value1 = object1[propertyName]; <br>var value2 = object2[propertyName]; <br>if(value2 < value1) { <br/>return -1; <br/>} else if(value2 > value1) { <br>return 1; <br>} else { <br>return 0; <br>} <br>} <br>} <br>console.info(JSON.stringify(data)); <br>//Usage method <br>data.sort(compare("value ")); <br>console.info(JSON.stringify(data)); <br></script>

JavaScript object array is sorted in descending order according to a certain attribute




Array of JavaScript objects arranged in descending order according to a certain attribute







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