how sort date by closest day to today first ?

how sort date by closest day to today first ?

eg2evereg2ever Posts: 3Questions: 1Answers: 0
edited March 2022 in DataTables

how sort date by closest day to today first ?
i want sort date to closest day to day first then desc

Like i have data ( 2022-03-25 / 2022-03-21 / 2022-03-30 / 2022-03-01 / 2022-03-03 / 2022-03-04 )

and say today is 2022-03-03

the result what i want is

2022-03-03
2022-03-04
2022-03-21
2022-03-25
2022-03-30
2022-03-01 _ if a day has passed move down_

thanks

Answers

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    You would need to use orthogonal data for that. Take a look at this example, it's returning a number for the strings to ensure they're in order. You would do something similar, doing a numerical difference in days between now and the date in the table,

    Colin

  • eg2evereg2ever Posts: 3Questions: 1Answers: 0

    **thanks colin
    **
    but not working 100%
    http://live.datatables.net/kohodopo/13/
    as u seen here last date is large than all and sorted it at last

    what's the problem ?

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    The code structure is good, the use of columns.render is spot on, but your logic and the switch statement looks very wrong:

           var data2 = new Date(milliseconds2);
              switch (data) {
                case today == data2:
                  return 0;
                case today < data2:
                  return 1;
                case today > data2:
                  return 2;
              }
            }
    

    You would be better using if ... else if ... else there instead.

    Colin

  • colincolin Posts: 15,112Questions: 1Answers: 2,583

    Another tip would be to convert the dates to UNIX time (seconds since 1970) as that would also be easier to compare. There's docs on the Moment.js site for that,

    Colin

Sign In or Register to comment.