Articles :: JavaScript :: Human friendly numeric sort
written by Toby Miller on September 1, 2005 September 1, 2005
If you find it as annoying as I do to have sorted arrays in javascript returned like this:
1
10
11
2
3
Instead of like this:
1
2
3
10
11
Then save this function some place. It will come in handy.
1:/**
2: * human friendly numeric sort
3: *
4: * @param array array to sort
5: * @return array sorted array
6: */
7:function sortarray(arr)
8:{
9: return arr.sort(
10: function(a, b)
11: {
12: return (a.match(/d+/) - b.match(/d+/));
13: }
14: );
15:}
16:
This function is also available in my Common JavaScript Library.
permalink
|
Natural Living (5) Heating & Cooling (1) Herbal Remedies (1) Personal (0) Family (1) Humor (11) Miscellaneous (1) Politics (5) Technology (2) System Administration (4) Linux (1) Solaris (0) Web Development (2) CSS (3) Design (1) Flash (1) JavaScript (11) PHP (1) CakePHP (1) Web Browsers (2) Firefox (1) Internet Exploder (0) Netscape (1)
|