Sort by multiple criteria for same column

Sort by multiple criteria for same column

islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1
edited November 2018 in Free community support

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

  • colincolin Posts: 15,237Questions: 1Answers: 2,598

    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

  • islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1
    edited November 2018

    @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 :

    0r0  -> 000
    0r1  -> 001
    0r4  -> 004
    0r6  -> 006
    0o0  -> 010
    0r24 -> 0024
    

    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 :

    0r0  -> 000
    0r24 -> 0024
    0r6  -> 006
    0r4  -> 004
    0r1  -> 001
    0o0  -> 010
    

    Any other ideas I can look into? :(

  • kthorngrenkthorngren Posts: 21,143Questions: 26Answers: 4,918

    I would try the Natural sorting plugin with the numeric format you are generating.

    Kevin

  • islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1

    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?

  • colincolin Posts: 15,237Questions: 1Answers: 2,598
    edited November 2018

    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

  • kthorngrenkthorngren Posts: 21,143Questions: 26Answers: 4,918

    it does sort with the 0r24 format (doesn't work with the numeric one).

    Seems to work with the numeric or 0r24 format here:
    http://live.datatables.net/cahucaxu/1/edit

    it sorts it asc, how to sort it desc using natural

    Looks like it sorts correctly in either direction in my example. However is seems like you want Red before Orange but alphabetically o is before r. 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

  • islamelshobokshyislamelshobokshy Posts: 99Questions: 20Answers: 1

    It's perfect, thank you both!

This discussion has been closed.