Sorting dilemma - none of the column types seem to work

edited February 2012
I have a column with data like:

50
100 of 100
10 of 75
55 of 100
78
5 of 20
65 of 250
432
...

I want to order them by the first number, so:
5 of 20
10 of 75
50
55 of 100
65 of 250
78
100 of 100
432

For aoColumns sType, I've tried:
- string-case
- null
- html
- string
- numeric

But nothing seems to sort properly. I'll typically get the 10 and 100 of 100 rows together since they both start with "1". And numeric likely doesn't work because I have strings in the data.

Any ideas?

Thanks!

Replies

Hi,

I am no master of DataTables (just started using it yesterday) but I think your solution is to write your own sorting comparator.

Custom sorting in data tables:
http://datatables.net/development/sorting#type_based

Your comparator can take the left and right values, split them both on " " (space) and then compare the first element returned from split numerically. A comparison function similar to

[code]
function compare(x,y) {
var x1 = Number(x.split(" ")[0]);
var y1 = Number(y.split(" ")[0]);
return ((x1 < y1) ? -1 : ((x1 > y1) ? 1 : 0));
}
[/code]
edited February 2012
You rock - that worked - thanks so much!

[code]jQuery.fn.dataTableExt.oSort['submissions-asc'] = function(x,y) {
var x1 = Number(x.split(" ")[0]);
var y1 = Number(y.split(" ")[0]);
return ((x1 < y1) ? -1 : ((x1 > y1) ? 1 : 0));
}
jQuery.fn.dataTableExt.oSort['submissions-desc'] = function(x,y) {
var x1 = Number(x.split(" ")[0]);
var y1 = Number(y.split(" ")[0]);
return ((x1 < y1) ? 1 : ((x1 > y1) ? -1 : 0));
}[/code]
This discussion has been closed.