Last active
August 29, 2015 13:57
-
-
Save 06b/9358119 to your computer and use it in GitHub Desktop.
Useful for sorting lists with javascript
This file contains hidden or 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
// Utility Sort functions - useful for sorting lists | |
// accending sort | |
function asc_sort(a, b) { | |
return ($(b).text()) < ($(a).text()) ? 1 : -1; | |
} | |
// decending sort | |
function dec_sort(a, b) { | |
return ($(b).text()) > ($(a).text()) ? 1 : -1; | |
} | |
// mixed alpha/numeric array sort - http://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array?lq=1 | |
function alphanum_sort(a, b) { | |
var charPart = [$(a).text().substring(0, 1), $(b).text().substring(0, 1)], | |
numPart = [$(a).text().substring(1) * 1, $(b).text().substring(1) * 1]; | |
if (charPart[0] < charPart[1]) return -1; | |
else if (charPart[0] > charPart[1]) return 1; | |
else { //(charPart[0] == charPart[1]){ | |
if (numPart[0] < numPart[1]) return -1; | |
else if (numPart[0] > numPart[1]) return 1; | |
} | |
} | |
// decending mixed alpha/numeric array sort | |
function dec_alphanum_sort(a, b) { | |
var charPart = [$(a).text().substring(0, 1), $(b).text().substring(0, 1)], | |
numPart = [$(a).text().substring(1) * 1, $(b).text().substring(1) * 1]; | |
if (charPart[0] > charPart[1]) return -1; | |
else if (charPart[0] < charPart[1]) return 1; | |
else { //(charPart[0] == charPart[1]){ | |
if (numPart[0] < numPart[1]) return -1; | |
else if (numPart[0] > numPart[1]) return 1; | |
} | |
} | |
//example: $(#lorem).sort(dec_alphanum_sort); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment