並べ替えるカスタムの非数値値があるため、ある種のカスタム並べ替え関数が必要になります。テーブルのカスタムソートルーチンは次のとおりです。
HTML:
<table id="service">
<tr><td class="sortBy" data-sortType="findNumber">Service</td><td class="sortBy" data-sortType="numeric">Price</td></tr>
<tr><td>S1</td><td>13 CHF</td></tr>
<tr><td>S2</td><td>Free</td></tr>
<tr><td>S3</td><td>Free</td></tr>
<tr><td>S4</td><td>40 CHF</td></tr>
</table>
コード:
$(".sortBy").click(function() {
var self = $(this);
var tbody = self.closest("tbody");
// determine which column was clicked on
var column = self.index();
// get the sort type for this column
var sortType = self.data("sortType");
var sortCells = [];
// get all rows other than the clicked on row
// find the right column in that row and
// get the sort key from it
$(this).closest("tr").siblings().each(function() {
var item = $(this).find("td").eq(column);
var val = item.text();
var matches;
if (sortType == "numeric") {
if (val.toLowerCase() == "free") {
val = 0;
} else {
val = parseInt(val, 10);
}
} else {
// find numbers in the cell
matches = val.match(/\d+/);
if (matches) {
val = parseInt(matches[0], 10);
} else {
val = 0;
}
}
sortCells.push({cell: item, data: val});
});
// sort by the key
sortCells.sort(function(a, b) {
return(a.data - b.data);
});
// reinsert into the table in sorted order
for (var i = 0; i < sortCells.length; i++) {
sortCells[i].cell.parent().appendTo(tbody);
}
});
作業デモ: http: //jsfiddle.net/jfriend00/3w9gQ/