我有包含名字,姓氏和年齡的json對象數組。我想根據年齡對數組進行排序。基於屬性排序json對象
<!DOCTYPE html>
<html>
<body>
<h2>Create Object from JSON String</h2>
<p>Original name: <span id="origname"></span></p>
<p>New name: <span id="newname"></span></p>
<p>Age: <span id="age"></span></p>
<script>
var employees = [
{ "firstName" : "John" , "lastName" : "Doe" , "age":"24" },
{ "firstName" : "Anna" , "lastName" : "Smith" , "age":"30" },
{ "firstName" : "Peter" , "lastName" : "Jones" , "age":"45" },
];
document.getElementById("origname").innerHTML=employees[0].firstName + " " + employees[0].lastName;
// Set new name
employees[0].firstName="Gilbert";
document.getElementById("newname").innerHTML=employees[0].firstName + " " + employees[0].lastName;
document.getElementById("age").innerHTML=employees[0].age;
</script>
</body>
</html>
也許這個答案可以幫助你http://stackoverflow.com/questions/881510/jquery-sorting-json-by-properties –
https://developer.mozilla.org/en- US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort – Prinzhorn
檢查此鏈接http://stackoverflow.com/questions/8175093/simple-function-to-sort-a-json-object-using-javascript –