Skip to content

Instantly share code, notes, and snippets.

@yehohanan7
Created August 9, 2016 11:47
Show Gist options
  • Save yehohanan7/f9f4ce16ab0f50eb132ad62ec9eef2c7 to your computer and use it in GitHub Desktop.
Save yehohanan7/f9f4ce16ab0f50eb132ad62ec9eef2c7 to your computer and use it in GitHub Desktop.
sort
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