Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/467.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript中的排序日/月数组_Javascript_Arrays_Sorting - Fatal编程技术网

Javascript中的排序日/月数组

Javascript中的排序日/月数组,javascript,arrays,sorting,Javascript,Arrays,Sorting,我正在尝试对日期数组进行从最新到最旧的排序,不幸的是list.sort(默认情况下)只对第一个数字进行排序。我的数组如下所示: var MyArray = ["13 Jun", "09 Jun", "25 Aug", "30 Jun", "13 Aug"]; 我试图为.sort创建一个引用函数,但整个过程让我有些困惑。有人能帮我吗 您可以传递一个函数进行排序以进行自定义排序: function sortDate (a,b) { var aa = a.split(' '); va

我正在尝试对日期数组进行从最新到最旧的排序,不幸的是list.sort(默认情况下)只对第一个数字进行排序。我的数组如下所示:

var MyArray = ["13 Jun", "09 Jun", "25 Aug", "30 Jun", "13 Aug"];

我试图为.sort创建一个引用函数,但整个过程让我有些困惑。有人能帮我吗

您可以传递一个函数进行排序以进行自定义排序:

function sortDate (a,b) {
    var aa = a.split(' ');
    var bb = b.split(' ');
    var months = {
        Jan : 1,
        Feb : 2,
        Mar : 3 /* you do the rest ... */
    }

    if (months[a[1]] > months[b[1]]) return 1;
    if (months[a[1]] < months[b[1]]) return -1;

    return parseInt(a[0],10) - parseInt(b[0],10);
}

MyArray.sort(sortDate);
函数排序日期(a,b){
var aa=a.分割(“”);
var bb=b.分割(“”);
风险值月份={
1月1日,
二月二号,,
Mar:3/*剩下的由你来做*/
}
如果(月[a[1]]>月[b[1]])返回1;
如果(月[a[1]<月[b[1]])返回-1;
返回parseInt(a[0],10)-parseInt(b[0],10);
}
MyArray.sort(sortDate);

您可以传递一个函数进行排序以进行自定义排序:

function sortDate (a,b) {
    var aa = a.split(' ');
    var bb = b.split(' ');
    var months = {
        Jan : 1,
        Feb : 2,
        Mar : 3 /* you do the rest ... */
    }

    if (months[a[1]] > months[b[1]]) return 1;
    if (months[a[1]] < months[b[1]]) return -1;

    return parseInt(a[0],10) - parseInt(b[0],10);
}

MyArray.sort(sortDate);
函数排序日期(a,b){
var aa=a.分割(“”);
var bb=b.分割(“”);
风险值月份={
1月1日,
二月二号,,
Mar:3/*剩下的由你来做*/
}
如果(月[a[1]]>月[b[1]])返回1;
如果(月[a[1]<月[b[1]])返回-1;
返回parseInt(a[0],10)-parseInt(b[0],10);
}
MyArray.sort(sortDate);

您必须将排序函数传递给sort方法,并定义一个日期解析函数,该函数创建日期的有序表示,如下所示:

var MyArray = ["13 Jun", "09 Jun", "25 Aug", "30 Jun", "13 Aug"];
MyArray.sort(function (a, b) {
    a = parseDate(a);
    b = parseDate(b);
    if (a < b) {
            return 1;
    } else if (a > b) {
            return -1;
    } else {
            return 0;
    }
});
var MyArray=[“6月13日”、“6月9日”、“8月25日”、“6月30日”、“8月13日];
排序(函数(a,b){
a=解析日期(a);
b=解析日期(b);
if(ab){
返回-1;
}否则{
返回0;
}
});

您必须将排序函数传递给sort方法,并定义一个日期解析函数,该函数创建日期的有序表示,如下所示:

var MyArray = ["13 Jun", "09 Jun", "25 Aug", "30 Jun", "13 Aug"];
MyArray.sort(function (a, b) {
    a = parseDate(a);
    b = parseDate(b);
    if (a < b) {
            return 1;
    } else if (a > b) {
            return -1;
    } else {
            return 0;
    }
});
var MyArray=[“6月13日”、“6月9日”、“8月25日”、“6月30日”、“8月13日];
排序(函数(a,b){
a=解析日期(a);
b=解析日期(b);
if(ab){
返回-1;
}否则{
返回0;
}
});

您必须将字符串解析为Date()对象,如果没有IE糟糕的日期字符串解析实现,这将非常简单。幸运的是,您可以跨浏览器一致地使用setDate()和setMonth(),它们都接受数字-1-31表示setDate(),0-11表示setMonth()。为月份名称设置对象映射将有所帮助

这对我很有用:

(function () { 
    // Set up our variables, 2 date objects and a map of month names/numbers
    var ad = new Date();
    var bd = new Date();
    var months = {
        Jan: 0,
        Feb: 1,
        Mar: 2,
        Apr: 3,
        May: 4,
        Jun: 5,
        Jul: 6, 
        Aug: 7,
        Sep: 8,
        Oct: 9,
        Nov: 10,
        Dec: 11
    };

    MyArray.sort(function (a,b) {
        // Split the text into [ date, month ]
        var as = a.split(' '),
            bs = b.split(' ');

        // Set the Date() objects to the dates of the items
        ad.setDate(as[0]);
        ad.setMonth(months[as[1]]);
        bd.setDate(bs[0]);
        bd.setMonth(months[bs[1]]);

        /* A math operation converts a Date object to a number, so 
           it's enough to just return date1 - date2 */
        return ad - bd;
    });
})();
//-> ["09 Jun", "13 Jun", "30 Jun", "13 Aug", "25 Aug"]

我为您设置了一个示例-

您必须将字符串解析为Date()对象,如果没有IE糟糕的日期字符串解析实现,这将非常简单。幸运的是,您可以跨浏览器一致地使用setDate()和setMonth(),它们都接受数字-1-31表示setDate(),0-11表示setMonth()。为月份名称设置对象映射将有所帮助

这对我很有用:

(function () { 
    // Set up our variables, 2 date objects and a map of month names/numbers
    var ad = new Date();
    var bd = new Date();
    var months = {
        Jan: 0,
        Feb: 1,
        Mar: 2,
        Apr: 3,
        May: 4,
        Jun: 5,
        Jul: 6, 
        Aug: 7,
        Sep: 8,
        Oct: 9,
        Nov: 10,
        Dec: 11
    };

    MyArray.sort(function (a,b) {
        // Split the text into [ date, month ]
        var as = a.split(' '),
            bs = b.split(' ');

        // Set the Date() objects to the dates of the items
        ad.setDate(as[0]);
        ad.setMonth(months[as[1]]);
        bd.setDate(bs[0]);
        bd.setMonth(months[bs[1]]);

        /* A math operation converts a Date object to a number, so 
           it's enough to just return date1 - date2 */
        return ad - bd;
    });
})();
//-> ["09 Jun", "13 Jun", "30 Jun", "13 Aug", "25 Aug"]

我为您设置了一个示例-

这里有一个解决方案,它不回答任何常量

var MyArray = ["13 Jun", "09 Jun", "25 Aug", "30 Jun", "13 Aug"];
var DateArray = [];
var SortedArray = [];
var dateBits;
for (var index = 0; index < MyArray.length; index++) {
 DateArray.push(new Date(MyArray[index] + " 2000"));
}
SortedArray = DateArray.sort();
MyArray = [];
for (var index = 0; index < SortedArray.length; index++) {
  dateBits = SortedArray[0].toDateString().substr(4, 6).split(" ");
  MyArray[index] = dateBits[1] + " " + dateBits[0];
}
var MyArray=[“6月13日”、“6月9日”、“8月25日”、“6月30日”、“8月13日];
var DateArray=[];
var-Darray=[];
var数据位;
对于(var index=0;index
这里有一个解决方案,它不回答任何常量

var MyArray = ["13 Jun", "09 Jun", "25 Aug", "30 Jun", "13 Aug"];
var DateArray = [];
var SortedArray = [];
var dateBits;
for (var index = 0; index < MyArray.length; index++) {
 DateArray.push(new Date(MyArray[index] + " 2000"));
}
SortedArray = DateArray.sort();
MyArray = [];
for (var index = 0; index < SortedArray.length; index++) {
  dateBits = SortedArray[0].toDateString().substr(4, 6).split(" ");
  MyArray[index] = dateBits[1] + " " + dateBits[0];
}
var MyArray=[“6月13日”、“6月9日”、“8月25日”、“6月30日”、“8月13日];
var DateArray=[];
var-Darray=[];
var数据位;
对于(var index=0;index