I have a function to sort rows alphabetically and it works pretty good, but if it contains numbers it will sort it a1, a10, a11, ..., a2, a20, ..., a3 instead of a1, a2, a3 etc.
function sort(element) {
var sortableList = element;
var listitems = $('tr', sortableList);
listitems.sort(function (a, b) {
return ($(a).find('td.myclass').text().toUpperCase() > $(b).find('td.myclass').text().toUpperCase()) ? 1 : -1;
});
sortableList.append(listitems);
}
Trying with this code form the example below. Not working yet:
function sort(element) {
var sortableList = element;
var listitems = $('tr', sortableList);
var word = /[a-z]/i,
digit = /\d+/;
listitems.sort(function (a, b) {
return +$(a).find('td.myclass').text().match(digit)[0] > +$(b).find('td.myclass').text().match(digit)[0] ? 1 : -1;
}).sort(function (a, b) {
return $(a).find('td.myclass').text().match(word)[0] > $(b).find('td.myclass').text().match(word)[0] ? 1 : -1;
});
sortableList.append(listitems);
}
Error: null is not an object (evaluating '$(b).find('td.myclass').text().match(digit)[0]')
listitemsare all strings or numbers it should not be a problem."80" > "9"and80 > 9will give you the same results. As long asaandbare of the same primitive type, should not be a problem. The issue arises only if one is a number and another is a string."80" > "9"and80 > 9will give you the same results.” — this is false.