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.
在第一种情况下,方法正常工作(返回按日期排序的数组),但在另一个jsp页面上它不起作用(返回相同的数组,接收。日期格式相同)。最奇怪的是在使用输出期间(“alert(a +”“+ b);”)vars“a”,“b”的值等于“Invalid Date”。但这一事实并不影响第一种情况。
2 个解决方案
#1
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
首先确保a.date和b.date是有效的日期字符串,可以创建Date对象。请参阅此内容以确定它们是否为有效的日期字符串: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;
};
#2
0
You can get timestamps from strings with Date.parse.
您可以使用Date.parse从字符串中获取时间戳。
This filters out invalid date input by testing with isFinite before sorting:
这通过在排序之前使用isFinite进行测试来过滤掉无效日期输入:
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'];
['9/8 / 2007,4:51:00','11 / 8 / 2001,1:43:00 PM','5/6/2019:9:12:00 AM','10 / 27 / 2013,2:12:00'','10 / 18 / 2004,1:06:00 PM','8/20 / 2000,4:18:00 AM','2015年8月17日,5: 55:00 PM','10 / 30/2000,3:44:00 PM','12 / 8 / 2002,1:45:00'','1/21 / 2000,11:11:00 AM' ,'6/10/2000,下午3:52:00,'12 / 17/2012,上午12:29:00','1/11/2000,12:54:00 PM','1/27 / 2007,11:14:00 PM','4/30 / 2000,8:07:00','7/26 / 201,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/203:3:51:00' ,',1/31 / 2000,1:43:00','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','2010年7月18日,3: 17:00 AM','2/5 / 2012,7:09:00','5/20 / 2003,5:44:00 PM','9/15 / 201,1:32:00 AM' ,'4/21 / 2001,11:14:00 AM','11/7/2005,上午9:18:00','7/16/201,上午9:21:00','10 / 13 / 2028,7:29:00'','7/6 / 2017,1:39:00 PM','7/26 / 2016,6:19:00 AM','2008年5月18日,12:下午45:00','3/21/2002,上午7:05:00','3/22/2000,3:51:00 PM','2014年9月6日,1: 34:00 AM','bob','4/8 / 2004,1:56:00 PM','12 / 5/2023,上午6:58:00','1/9 / 2001,9:36 :00 PM','10 / 28/2003,7:50:00 PM','10 / 26/2022,2:06:00 PM','7/5 / 20,20:57:00', '7/30 / 2001,5:04:00','6/19 / 2006,4:24:00','4/27 / 2011,12:12:00 PM','1/1 / 2000,下午6:41:00'];
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 */
946770060000,947613240000,948471060000,949344180000,950189100000,953754660000,957139620000,960666720000,966759480000,972935040000,979094160000,987866040000,996527040000,1005244980000,1016708700000,1027969440000,1039373100000,1053467040000,10673.85亿,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)
/ *返回值:(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 */
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,2 / 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,2000 / 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,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,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,18 / 17 / 2015,5:55:00 PM,7/26 / 2016,6:19:00 AM,7/6 / 2017,1:39:00 PM,7/16/201,上午9:21:00,7 / 26 / 201,10:15:00 PM,8/4 / 2020,11:31:00 AM,9/15 / 201,2:32:00 AM,10/26 / 2022,下午2:06:00,12 / 5 / 2023,6:58:00 AM,2/13/209:6:05:00 PM,4/27 / 20,20:10:00 PM,7/5 / 20,20:57:00 AM ,10/13 / 2008,7:29:00 AM,1/24 / 30,3:51:00 AM,5/6/2019:9:12:00 * /
#1
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
首先确保a.date和b.date是有效的日期字符串,可以创建Date对象。请参阅此内容以确定它们是否为有效的日期字符串: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;
};
#2
0
You can get timestamps from strings with Date.parse.
您可以使用Date.parse从字符串中获取时间戳。
This filters out invalid date input by testing with isFinite before sorting:
这通过在排序之前使用isFinite进行测试来过滤掉无效日期输入:
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'];
['9/8 / 2007,4:51:00','11 / 8 / 2001,1:43:00 PM','5/6/2019:9:12:00 AM','10 / 27 / 2013,2:12:00'','10 / 18 / 2004,1:06:00 PM','8/20 / 2000,4:18:00 AM','2015年8月17日,5: 55:00 PM','10 / 30/2000,3:44:00 PM','12 / 8 / 2002,1:45:00'','1/21 / 2000,11:11:00 AM' ,'6/10/2000,下午3:52:00,'12 / 17/2012,上午12:29:00','1/11/2000,12:54:00 PM','1/27 / 2007,11:14:00 PM','4/30 / 2000,8:07:00','7/26 / 201,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/203:3:51:00' ,',1/31 / 2000,1:43:00','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','2010年7月18日,3: 17:00 AM','2/5 / 2012,7:09:00','5/20 / 2003,5:44:00 PM','9/15 / 201,1:32:00 AM' ,'4/21 / 2001,11:14:00 AM','11/7/2005,上午9:18:00','7/16/201,上午9:21:00','10 / 13 / 2028,7:29:00'','7/6 / 2017,1:39:00 PM','7/26 / 2016,6:19:00 AM','2008年5月18日,12:下午45:00','3/21/2002,上午7:05:00','3/22/2000,3:51:00 PM','2014年9月6日,1: 34:00 AM','bob','4/8 / 2004,1:56:00 PM','12 / 5/2023,上午6:58:00','1/9 / 2001,9:36 :00 PM','10 / 28/2003,7:50:00 PM','10 / 26/2022,2:06:00 PM','7/5 / 20,20:57:00', '7/30 / 2001,5:04:00','6/19 / 2006,4:24:00','4/27 / 2011,12:12:00 PM','1/1 / 2000,下午6:41:00'];
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 */
946770060000,947613240000,948471060000,949344180000,950189100000,953754660000,957139620000,960666720000,966759480000,972935040000,979094160000,987866040000,996527040000,1005244980000,1016708700000,1027969440000,1039373100000,1053467040000,10673.85亿,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)
/ *返回值:(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 */
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,2 / 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,2000 / 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,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,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,18 / 17 / 2015,5:55:00 PM,7/26 / 2016,6:19:00 AM,7/6 / 2017,1:39:00 PM,7/16/201,上午9:21:00,7 / 26 / 201,10:15:00 PM,8/4 / 2020,11:31:00 AM,9/15 / 201,2:32:00 AM,10/26 / 2022,下午2:06:00,12 / 5 / 2023,6:58:00 AM,2/13/209:6:05:00 PM,4/27 / 20,20:10:00 PM,7/5 / 20,20:57:00 AM ,10/13 / 2008,7:29:00 AM,1/24 / 30,3:51:00 AM,5/6/2019:9:12:00 * /