Skip to content

Month: March 2015

Sorting an array of Objects in JavaScript

If you have an array in JavaScript that doesn’t contain just simple numeric or string values, but objects with properties instead: var employees=[]; employees[0] = {name:”Petar”, age:32}; employees[1] = {name:”Nikola”, age:17}; employees[2] = {name:”Milutin”, age:58}; employees[3] = {name:”Slavisa”, age:62}; The employees array above is an array of objects with properties of different data types, string and numeric. The sort() method can be used to sort the array based on the values of one of these properties, such as sorting the array by name or age. The basic idea is to… Read more Sorting an array of Objects in JavaScript