Skip to content

Instantly share code, notes, and snippets.

@Victa
Created May 24, 2011 15:04
Show Gist options
  • Save Victa/988876 to your computer and use it in GitHub Desktop.
Save Victa/988876 to your computer and use it in GitHub Desktop.
Extends JS array sort functions
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