Created
September 26, 2011 11:22
-
-
Save fengelz/1242047 to your computer and use it in GitHub Desktop.
Sorting An Array In Javascript
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
var arr = ["Anne", "Carl", "Benny"]; | |
arr.sort(desc); //returns "Carl","Benny","Anne" | |
arr.sort(asc); //returns "Anne","Benny","Carl" | |
alert(arr); | |
function desc(a, b) | |
{ | |
if(a.toLowerCase() > b.toLowerCase()){return -1;} | |
else {return 1;} | |
} | |
function asc(a, b) | |
{ | |
if(a.toLowerCase() < b.toLowerCase()){return -1;} | |
else {return 1;} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment