How to sort column().data() by another date column?

How to sort column().data() by another date column?

sumnonesumnone Posts: 4Questions: 1Answers: 0

Hi, I'm using a piece of code like the following to get an array of totals for chartjs:

var totalsArray = api.column(15, { page: 'current' }).data();

It works fine like that. However, is there any way to sort this data by another column? In this case a date column? I saw the examples for columns().sort() but don't see any parameters for sorting by anything other than alphabetically. I basically want the chart to never change and always be sorted by the same date column ascending.

I really appreciate any help.


  • kthorngrenkthorngren Posts: 9,735Questions: 25Answers: 2,242
    edited May 2019

    You can include the selector-modifie of {order:'current'} as shown here:

    EDIT: Assuming you have the table sorted by the date column you want.


  • sumnonesumnone Posts: 4Questions: 1Answers: 0
    edited May 2019

    Thanks for the reply. I'm not sure what you mean... I'm displaying totals in column 15... but want it ordered by date in column 9. {order:'current'} seems to be the default setting?

    I also just tried this:
    totalsArray = totalsArray.order([9, 'asc']);

    That sets the correct order on the chart like I want, but also highjacks the order of the table too. How can I separate these from each other?

  • colincolin Posts: 9,165Questions: 0Answers: 1,532
    edited May 2019

    Hi @sumnone ,

    You can't change the type - if it's recognised as a date field, that all the ordering will be in that format. The function you mentioned, sort(), works on the data, so you need

    var totalsArray = api.column(15, { page: 'current' }).data().sort();

    See here,



  • sumnonesumnone Posts: 4Questions: 1Answers: 0

    Hi, thanks for the reply. Column 15 is a decimal (and will remain a decimal) that I am trying to sort by column 9 which is a date (and will remain a date). Therefore, I am trying to always sort column 15 in chronological order ascending, but on the chart only. I want all the sort column functions to remain working in my table, while the chart always orders everything in chronological order.

    Essentially, I'm calling the api.column() in my datatables footerCallback and piecing together my chart data there. But, any manipulation of the api data, equally affects the original table data too.

    I could make another call to the server and grab a second set of json data and break the chart data into it's own thing (outside the datatables function), but that's not optimal, although really easy.

    It's looking like it's not possible inherently in datatables without maybe cloning the datatables data array and then manipulating that second data array through pure javascript?

    I kind of gave up on it for the moment. If I ever figure out something clean, I'll post it back here. Thanks again for the replies.

  • sumnonesumnone Posts: 4Questions: 1Answers: 0

    I was looking to solve another issue and ended up finding the solution to this one. I used the example code from the following link to split out the array:

    Not only does it sort the chart by date always (it never moves, always looks exactly the same no matter how I sort datatables), but the whole thing just seems to perform better now too.

This discussion has been closed.