Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.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 使用日期和字符串angularjs对对象数组进行排序_Javascript_Angularjs_Sorting - Fatal编程技术网

Javascript 使用日期和字符串angularjs对对象数组进行排序

Javascript 使用日期和字符串angularjs对对象数组进行排序,javascript,angularjs,sorting,Javascript,Angularjs,Sorting,我有一个就业记录对象数组,假设它有以下记录: var records = [ {name: "Sample", monthTo: "NOV", yearTo: "1960"}, {name: "Sample2", monthTo: "JAN", yearTo: "2016"}, {name: "Sample3", monthTo: "DEC", yearTo: "2017"}, ]; 如果我想在最新版本中对此进行排序,我可以做: f

我有一个就业记录对象数组,假设它有以下记录:

var records = [
        {name: "Sample",  monthTo: "NOV", yearTo: "1960"},
        {name: "Sample2",  monthTo: "JAN", yearTo: "2016"},
        {name: "Sample3",  monthTo: "DEC", yearTo: "2017"},
    ];
如果我想在最新版本中对此进行排序,我可以做:

function sortEmpHistoryByLatest(){

        records.sort(function(a,b) {


                var aDate = new Date(a.getYearTo()+"-"+a.getMonthTo()+"-01").getDate();
                var bDate = new Date(b.getYearTo()+"-"+b.getMonthTo()+"-01").getDate();

                return aDate - bDate

        });
    }
这很好,但是如果我将
作为记录而不是月份或年份格式呈现,该怎么办?比如说:

var records2 = [
        {name: "Sample",  monthTo: "NOV", yearTo: "1960"},
        {name: "Sample2",  monthTo: "Present", yearTo: "Present"},
        {name: "Sample3",  monthTo: "DEC", yearTo: "2017"},
    ];

如果列表中的第一个是
Present
,如何对其进行排序?

您可以将数组分为两个数组-一个只带日期,另一个带“Present”标记。然后只对第一个数组排序并合并它们

您可以使用下一个代码拆分它们:

var records_with_dates = records.filter(function(record){return record.monthTo!=='Present';});
var sorted_records_with_dates = records_with_dates.sort(// your sort func);
var present_records = records.filter(function(record){return record.monthTo==='Present';});

var sorted_records = present_records.concat(sorted_records_with_dates);

您可以如下所示:用当前年份和月份替换当前

而且你可以工作


嗨@yassi,这回答了你的问题吗?
var sorted;
var months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'];
var today = new Date();
var mm = today.getMonth();
var yyyy = today.getFullYear();
var mmStr = months[mm];
console.log('currentMonth:' + mmStr)

function sortEmpHistoryByLatest() {
  sorted = records.sort(function(a, b) {

    var monthA = a.monthTo == "Present" ? mm : months.indexOf(a.monthTo);
    var yearA = a.yearTo == "Present" ? yyyy : a.yearTo;
    var monthB = b.monthTo == "Present" ? mm : months.indexOf(b.monthTo);
    var yearB = b.yearTo == "Present" ? yyyy : b.yearTo;

    var aDate = new Date(yearA, monthA);
    var bDate = new Date(yearB, monthB); 
    console.log('A'+aDate+' yearMonth'+yearA+''+monthA);
    console.log('B'+aDate+' yearMonth'+yearB+''+monthB)
    return bDate - aDate; //Ascending  order
    //return aDate - bDate;//for Descending  order
  });
}