I fund a very nice sorting function here
How may I sort a list alphabetically using jQuery?
works almost perfect except that it looks like sorts by first charachter only , for example
if you have something like
0-5000
5001-10000
10001-15000
15001-20000
the order will look like
0-5000
10001-15000
15001-20000
5001-10000
instead
0-5000
5001-10000
10001-15000
15001-20000
I redid the function for every select in the form
var mylist = $('#myformid select');
$(mylist).each(function (sorter, elm) {
var listitems = $(this).children('option.myoptionclass').get();
listitems.sort(function(a, b) {
var compA = $(a).text().toUpperCase();
var compB = $(b).text().toUpperCase();
return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
})
$.each(listitems, function(idx, itm) {
$(this).parent().append(itm);
});
});
here is live example
any help is welcome!
I editted your fiddle: http://jsfiddle.net/CBD33/2/
I’m using
parseIntso the comparison looks like this now:If the first character of the string value given to
parseIntis a numeral, it will parse everything up to the first non-digit character. If the first character isn’t a numeral, it’ll return NaN. My use of||there is rather lazy, but I’m sure you can come up with a better condition.