Sort by multiple criteria for same column
Sort by multiple criteria for same column
I have a column that looks like this :
0 // Red colored
0 // Red colored
0 (1) // Orange colored
0 (2) // Red colored
0 // Orange colored
0 // Red colored
0 (24) // Orange colored
0 (4) // Red colored
0 // Orange colored
0 (3) // Orange colored
1
1
1 (7)
2
2 (3)
2
... etc
I would get the following order :
0 // Red colored
0 // Red colored
0 // Red colored
0 (24) // Red colored
0 (4) // Red colored
0 (2) // Red colored
0 // Orange colored
0 // Orange colored
0 (4) // Orange colored
0 (1) // Orange colored
1
1
1 (7)
2
2 (3)
2
... etc
I want to order this column in datatables using these 3 criteria : first order by number (0's), then by color (red first), then by number un parantheses (the highest first).
I've got in each data-order
the following format : 0r24
which means the first number is zero, the color is red, the number in the parantheses is 24. Repeat for all others.
I tried to play with the columnDefs
attribute of Datatables to get my result by doing the following :
"columnDefs": [
{
"type": "num-fmt",
"targets": 7
}, {
"targets": [7],
"render": function ( data, type, row, meta ) {
return type === 'sort' ?
row[7]['@data-order'].replace(/r/g,'0').replace(/o/g,'1') : row[7]['@data-order'];
}
}
]
Here I am trying to replace the letters o
and r
with 0
and 1
to be able to have an all numeric value for it to order upon, but it doesn't work. Any idea how to make this work and do the following as I explained?
Replies
Hi @islamelshobokshy ,
I'd be tempted to create a sorting plugin and take an approach similar to Chapter which came out of this thread.
For you, break it down into the three sections, mapping the colour into a number, and join them up to give you one long numeric string.
Hope that helps,
Cheers,
Colin
@colin I tried the idea of transforming an r into a 0 and an o into a 1 for exemple, but it doesn't work either. The two digits reds are always at the end after the oranges... Here's a small exemple :
And that 0024 is being put after the 010 which for me is illogical.
I think he's removing the zeroes and giving me the output of 10 is bigger than 24... And if I try to sort is as a string it won't work either as he'll consider 0024 bigger than 004 as 2<4.
And even if it worked, I'd wanted the 24 to be the first and not the last to have this :
Any other ideas I can look into?
I would try the Natural sorting plugin with the numeric format you are generating.
Kevin
Thank you so much @kthorngren it does sort with the
0r24
format (doesn't work with the numeric one).Buy it sorts it asc, how to sort it desc using natural? I don't think it's possible. So I can't have exactly the format I'm looking for?
They need to be equal length, that's what the
makeFiller()
function is doing in the Chapter example I linked to.So, work out the maximum expected width, assuming two digits, then
Orange = 01
Red = 02
0 (24) // Orange colored ---> '000124'
0 (4) // Red colored ---> '000204'
1 (7) ---> '010007"
etc
Seems to work with the numeric or
0r24
format here:http://live.datatables.net/cahucaxu/1/edit
Looks like it sorts correctly in either direction in my example. However is seems like you want Red before Orange but alphabetically
o
is beforer
. So I think natrual.js works correctly its just not the order you want.Also Colin's padding method would be good too.
If you still have difficulties please update or build an example that we can help you with.
Kevin
It's perfect, thank you both!