Created
August 9, 2016 11:47
-
-
Save yehohanan7/f9f4ce16ab0f50eb132ad62ec9eef2c7 to your computer and use it in GitHub Desktop.
sort
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment