<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