Created
May 24, 2011 15:04
-
-
Save Victa/988876 to your computer and use it in GitHub Desktop.
Extends JS array sort functions
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
Array.prototype.sortIntAsc = function(field){ | |
this.sort(function(a,b){ | |
var v1 = parseInt(a[field]); | |
var v2 = parseInt(b[field]); | |
if (v1<v2) return -1; | |
if (v1>v2) return 1; | |
return 0; | |
}); | |
} | |
Array.prototype.sortIntDesc = function(field){ | |
this.sort(function(a,b){ | |
var v1 = parseInt(a[field]); | |
var v2 = parseInt(b[field]); | |
if (v1<v2) return 1; | |
if (v1>v2) return -1; | |
return 0; | |
}); | |
} | |
Array.prototype.sortStringAsc = function(field){ | |
this.sort(function(a,b){ | |
var v1 = a[field].toLowerCase(); | |
var v2 = b[field].toLowerCase(); | |
if (v1<v2) return -1; | |
if (v1>v2) return 1; | |
return 0; | |
}); | |
} | |
Array.prototype.sortStringDesc = function(field){ | |
this.sort(function(a,b){ | |
var v1 = a[field].toLowerCase(); | |
var v2 = b[field].toLowerCase(); | |
if (v1<v2) return 1; | |
if (v1>v2) return -1; | |
return 0; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment