I have a table with column names inside <th> and only numbers inside <td>.
When I click on the <th> the sorting algoritm sorts values asc or desc.
.sortElements(function (a, b) {
return $.text([a]) > $.text([b]) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
}
This sorts the text but there is the eternal string vs number problem. This sorts as string so 10 is smaller than 2 so I did this
.sortElements(function (a, b) {
return parseInt($.text([a]),10) > parseInt($.text([b]),10) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
}
This is sorting in the correct way (correct for my needs) if you have only numbers and 10 is bigger than 2. Now I ask you... Is there a better way to do this?
What about if you have also letters inside <td>? What must be done? This is for someone who needs this to work correctly no matter what is inside <td>...?
You can use the Number() constructor for better readability
.sortElements(function (a, b) {
return Number($.text([a])) > Number($.text([b])) ?
inverse ? -1 : 1
: inverse ? 1 : -1;
}