我是Javascript和jquery的新手。我想按照升序和降序排列从items []数组到item1 []和item2 []数组的日期。
var items = ["01-Jun-17", "03-Jun-17", "03-May-17", "05-Jun-17", "05-Jun-17", "18-May-17"];
Output:
/*----logic for Ascending:::------ *\
var item1=["03-May-17","18-May-17","01-Jun-17","03-Jun-17","05-Jun-17","05-Jun-17"];
/*----- logic for Descending:::------ *\
var item2=["05-Jun-17","05-Jun-17","03-Jun-17","01-Jun-17","18-May-17","03-May-17"];
答案 0 :(得分:1)
如果您要求对数组进行排序,那么您可以将字符串转换为Date,然后对其应用sort函数。
var items = ["01-Jun-17", "03-Jun-17", "03-May-17", "05-Jun-17", "05-Jun-17", "18-May-17"];
items.sort((d1,d2) => new Date(d1) - new Date(d2) > 0);
console.log("Ascending: ",items);
console.log("Descending: ",items.reverse());