Sorting with preprocessing?

Sorting with preprocessing?

hd1hd1 Posts: 1Questions: 1Answers: 0

I have a list of dates -- like February 5, 2024 23:04:52 in a datatable-powered table -- that I'd like to sort. I think I'll need to do the following:

  1. Convert the column to epoch timestamps.
  2. Sort the epoch values.
  3. Convert back to the format in the question.

I guess what I'm trying to find is a way to convert the relevant column to epoch time or sort on an attribute value in the relevant HTML tag. Calculating the epoch equivalent is done using moment.($('tr > td[3]').text().replace(' ', 'T')).valueOf(). I can store this value as an attribute, but how do I tell sort() to use that as the comparator? Thanks for the help, in advance.

Answers

  • colincolin Posts: 15,240Questions: 1Answers: 2,599

    You can use orthogonal data data to do that. In this example, a string is being converted into a number which is then used for sorting. You can use columns.render to do something similar, such as for sort return that epoch timestamp calculated by Moment.js.

    Colin

  • kthorngrenkthorngren Posts: 21,330Questions: 26Answers: 4,951

    Another option is to use the sorting plugin described in this blog.

    Kevin

Sign In or Register to comment.