var employees = [new Employee("e1", 28), new Employee("e2", 31), new Employee("e3", 25)] function Employee(name, age) { this.name = name this.age = age } function sort(employees) { if (employees == null || employees.length == 0) { return employees } //copy the array to avoid side effect and hence maintain "sort" as a pure function return employees.slice(0).sort(function(lhs, rhs) { return lhs.age - rhs.age }) } console.log(sort(employees))