Created
October 28, 2015 16:10
-
-
Save jzi96/daa9e1053a541cb6aa6b to your computer and use it in GitHub Desktop.
This is a sortfunction implemented with typescript
This file contains 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
function sort<T>(sortItems: T[], key: (item: T) => any): T[]{ | |
var res = sortItems.slice(0); | |
res.sort(function (a, b) { | |
var akey = key(a); | |
var bkey = key(b); | |
return akey > bkey ? 1 : akey < bkey ? -1 : 0; | |
}); | |
return res; | |
} | |
var t = [ | |
{name:"my", price: 5.0}, {name:"test", price:56.6}, | |
]; | |
sort(t, x=> x.name); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment