0

For sorting array by date is used following method:

function sortDate(sortValues)
{
  sortValues.sort(function (a,b)
  {
    a=new Date(a.date);
    b=new Date(b.date);
    if (a<b)
    {
      return -1;
    }
    if (a>b)
    {
      return 1;
    }
    return 0; 
  })
  return sortValues;
}

This method works oddly:

In first case method works correctly (returns array sorted by date), but on another jsp-page it doesn't work (returns the same array, which receives. Format of dates is identical). The most strange thing is during of usage outputs ("alert(a+" "+b);") values of vars "a", "b" are equal "Invalid Date". But this fact doesn't affect in first case.

slider
  • 349
  • 2
  • 17
  • 1
    Keep in mind that Java is not the same as JavaScript. JavaScript is commonly shortend to `js` to avoid confusion with `Java Server Pages` (`jsp`). – Halcyon May 16 '14 at 13:12
  • Verify that `a.date` and `b.date` are valid date strings. JavaScript seems to thing they are not. – Halcyon May 16 '14 at 13:13
  • definately, you are not able to send right `sortValues` to the jsp page. `invalid date` in the output seems to be saying that. – Ashish Negi May 16 '14 at 13:24
  • You can find some useful answers to this topic here: **[Sort Javascript Object Array By Date](http://stackoverflow.com/a/26759127/2247494)** – jherax Nov 05 '14 at 15:29

2 Answers2

0

First make sure that a.date and b.date are valid date strings, which can create a Date object. Refer this to find out whether they are valid date strings : link

Then try this.

function sortDate(sortValues)
{
    sortValues.sort(function(a,b){
        var date_a=new Date(a.date).getTime();
        var date_b=new Date(b.date).getTime();
        return date_a > date_b;
    }
    return sortValues;
};
Grainier
  • 1,595
  • 2
  • 16
  • 28
0

You can get timestamps from strings with Date.parse.

This filters out invalid date input by testing with isFinite before sorting:

function sortDates(arr){
    return arr.map(Date.parse).filter(isFinite).sort(function(a, b){
        return a-b
    });
}

//test:

var A1=

['9/8/2007, 4:51:00 PM', '11/8/2001, 1:43:00 PM', '5/6/2031, 9:12:00 AM', '10/27/2013, 2:12:00 AM', '10/18/2004, 1:06:00 PM', '8/20/2000, 4:18:00 AM', '8/17/2015, 5:55:00 PM', '10/30/2000, 3:44:00 PM', '12/8/2002, 1:45:00 PM', '1/21/2000, 11:11:00 AM', '6/10/2000, 3:52:00 PM', '12/17/2012, 12:29:00 AM', '1/11/2000, 12:54:00 PM', '1/27/2007, 11:14:00 PM', '4/30/2000, 8:07:00 PM', '7/26/2019, 10:15:00 PM', '2/10/2000, 8:25:00 AM', '1/26/2009, 12:13:00 PM', '4/29/2005, 10:38:00 PM', '1/24/2030, 3:51:00 AM', '1/31/2000, 1:43:00 PM', '10/8/2009, 1:33:00 AM', '8/4/2020, 11:31:00 AM', '4/27/2026, 10:10:00 PM', '2/13/2025, 6:05:00 PM', '7/29/2002, 3:04:00 PM', '7/18/2010, 3:17:00 AM', '2/5/2012, 7:09:00 PM', '5/20/2003, 5:44:00 PM', '9/15/2021, 1:32:00 AM', '4/21/2001, 11:14:00 AM', '11/7/2005, 9:18:00 AM', '7/16/2018, 9:21:00 AM', '10/13/2028, 7:29:00 AM', '7/6/2017, 1:39:00 PM', '7/26/2016, 6:19:00 AM', '5/18/2008, 12:45:00 PM', '3/21/2002, 7:05:00 AM', '3/22/2000, 3:51:00 PM', '9/6/2014, 1:34:00 AM', 'bob', '4/8/2004, 1:56:00 PM', '12/5/2023, 6:58:00 AM', '1/9/2001, 9:36:00 PM', '10/28/2003, 7:50:00 PM', '10/26/2022, 2:06:00 PM', '7/5/2027, 8:57:00 AM', '7/30/2001, 5:04:00 PM', '6/19/2006, 4:24:00 PM', '4/27/2011, 12:12:00 PM', '1/1/2000, 6:41:00 PM'];

var A2=sortDates(A1);

/* returned value: (Array)

946770060000, 947613240000, 948471060000, 949344180000, 950189100000, 953754660000, 957139620000, 960666720000, 966759480000, 972935040000, 979094160000, 987866040000, 996527040000, 1005244980000, 1016708700000, 1027969440000, 1039373100000, 1053467040000, 1067385000000, 1081446960000, 1098119160000, 1114828680000, 1131373080000, 1150748640000, 1169957640000, 1189284660000, 1211129100000, 1232989980000, 1254979980000, 1279437420000, 1303920720000, 1328486940000, 1355722140000, 1382854320000, 1409981640000, 1439848500000, 1469528340000, 1499362740000, 1531747260000, 1564193700000, 1596555060000, 1631683920000, 1666807560000, 1701777480000, 1739487900000, 1777342200000, 1814792220000, 1855049340000, 1895475060000, 1935839520000 */

You can then convert the timestamps back to dates or strings, if needed.

A2.map(function(itm){   
    return new Date(itm).toLocaleString();
}).join(', ');

/* returned value: (String)

1/1/2000, 6:41:00 PM, 1/11/2000, 12:54:00 PM, 1/21/2000, 11:11:00 AM, 1/31/2000, 1:43:00 PM, 2/10/2000, 8:25:00 AM, 3/22/2000, 2:51:00 PM, 4/30/2000, 8:07:00 PM, 6/10/2000, 3:52:00 PM, 8/20/2000, 4:18:00 AM, 10/30/2000, 2:44:00 PM, 1/9/2001, 9:36:00 PM, 4/21/2001, 11:14:00 AM, 7/30/2001, 5:04:00 PM, 11/8/2001, 1:43:00 PM, 3/21/2002, 6:05:00 AM, 7/29/2002, 3:04:00 PM, 12/8/2002, 1:45:00 PM, 5/20/2003, 5:44:00 PM, 10/28/2003, 6:50:00 PM, 4/8/2004, 1:56:00 PM, 10/18/2004, 1:06:00 PM, 4/29/2005, 10:38:00 PM, 11/7/2005, 9:18:00 AM, 6/19/2006, 4:24:00 PM, 1/27/2007, 11:14:00 PM, 9/8/2007, 4:51:00 PM, 5/18/2008, 12:45:00 PM, 1/26/2009, 12:13:00 PM, 10/8/2009, 1:33:00 AM, 7/18/2010, 3:17:00 AM, 4/27/2011, 12:12:00 PM, 2/5/2012, 7:09:00 PM, 12/17/2012, 12:29:00 AM, 10/27/2013, 2:12:00 AM, 9/6/2014, 1:34:00 AM, 8/17/2015, 5:55:00 PM, 7/26/2016, 6:19:00 AM, 7/6/2017, 1:39:00 PM, 7/16/2018, 9:21:00 AM, 7/26/2019, 10:15:00 PM, 8/4/2020, 11:31:00 AM, 9/15/2021, 1:32:00 AM, 10/26/2022, 2:06:00 PM, 12/5/2023, 6:58:00 AM, 2/13/2025, 6:05:00 PM, 4/27/2026, 10:10:00 PM, 7/5/2027, 8:57:00 AM, 10/13/2028, 7:29:00 AM, 1/24/2030, 3:51:00 AM, 5/6/2031, 9:12:00 AM */

kennebec
  • 94,076
  • 30
  • 99
  • 125