Need to sort on time

Need to sort on time

jenkojenko Posts: 6Questions: 0Answers: 0
edited December 2009 in General
I have a "time" column with data like
"4:30 PM"
"8:30 AM"
"10:30 AM"

Unfortunately, when I sort on this column, it comes out as above, i.e., "4:30 PM" comes above "8:30 AM". Is there any way to fix this? I can format the data any way I want, so handling it on the data-creation end is not a problem -- I just don't know how to format it to make it work. Preferably, I don't want to have to use "military time", e.g., "16:30" for 4:30 PM.

TIA!

Replies

  • allanallan Posts: 61,853Questions: 1Answers: 10,134 Site admin
    Hi jenko,

    If you are able to format the string as you wish, I'd suggest that you make use of the 'hidden title numeric' sorting plug-in: http://datatables.net/plug-ins/sorting#hidden_title . What to do is have your output something like this:

    4:30 PM

    So - you have the 24 hour clock, without the separator, as the number which will be sorted upon. To see how to use sorting plug-ins see this example: http://datatables.net/examples/api/sorting_plugin.html . You'll need to set the sType for the time column since there isn't an automatic type detection for this data type.

    There are other ways to do it as well (write a sorting plug-in which deals with time in a 12 hour clock etc), but this is probably the quickest when you have control over the formatting.

    Hope this helps,
    Allan
  • jenkojenko Posts: 6Questions: 0Answers: 0
    Awesome, I was hoping there'd be an easy way given my control over the output, and there was. Thanks!
This discussion has been closed.