猿问

Array.sort() 比较函数返回未定义

我正在尝试从最旧到最新对字符串日期数组进行排序。我设置了几个比较函数,但控制台说的a是undefined. 出了什么问题?


//Sort an array of dates in this format

const dates = [

'10',

'23 Apr 2018',

'01 Jun 1943',

'05 Aug 2055',

'22 Sep 1902'

'18 Aug 1970',

'01 Jan 1940',

'08 Mar 2018',

'11 Feb 1982',

'17 Mar 1927',  

];


//remove the data that is not in the correct format

const cleanedDates = dates.filter(date => date.length === 11);


//isolate the day, convert to number

const getDay = (str) => {

  return parseInt(str.slice(0,2));

};


//create a dictionary of months

const monthDict = {

  Jan: 1,

  Feb: 2,

  Mar: 3,

  Apr: 4,

  May: 5,

  Jun: 6,

  Jul: 7,

  Aug: 8,

  Sep: 9,

  Oct: 10,

  Nov: 11,

  Dec: 12

};


//get the month value via dictionary

const getMonth = (str) => {

  const month = str.slice(3,6);

  return monthDict[month];

};


//get the year, convert to number

const getYear = (str) => {

  return parseInt(str.slice(7));

}


//comparison helper functions


//compare day

const compareDay = (a,b) => {

  if (getDay(a) < getDay(b)) {

    return -1;

  } else if (getDay(a) === getDay(b)) {

    return 0;

  }

  } else if (getDay(a) > getDay(b)) {

    return 1;

  }

};


//compare month

const compareMonth = (a,b) => {

  if (getMonth(a) < getMonth(b)) {

    return -1

  } else if (getMonth(a) === getMonth(b)) {

    compareDay(a,b);

  } else if (getMonth(a) > getMonth(b)) {

    return 1;

  }

};


//compare year

const compareYear = (a,b) => {

  if (getYear(a) < getYear(b)) {

    return -1;

  } else if (getYear(a) === getYear(b)) {

    compareMonth(a,b);

  }

  } else if (getYear(a) > getYear(b)) {

    return 1

  }

};


//sort array

const sortedArray = cleanedDates.sort((a,b) => compareYear(a,b));


console.log(sortedArray);


慕的地10843
浏览 176回答 2
2回答

holdtom

你的语法不正确。其余的对我有用:)。,当您const date对 value进行操作时,您会错过一个22 Sep 1902。}当你执行 else if 时,两个位置有额外的。修复这将使它工作://Sort an array of dates in this formatconst dates = [&nbsp; &nbsp; '10',&nbsp; &nbsp; '23 Apr 2018',&nbsp; &nbsp; '01 Jun 1943',&nbsp; &nbsp; '05 Aug 2055',&nbsp; &nbsp; '22 Sep 1902',&nbsp; &nbsp; '18 Aug 1970',&nbsp; &nbsp; '01 Jan 1940',&nbsp; &nbsp; '08 Mar 2018',&nbsp; &nbsp; '11 Feb 1982',&nbsp; &nbsp; '17 Mar 1927'];//remove the data that is not in the correct formatconst cleanedDates = dates.filter(date => date.length === 11);//isolate the day, convert to numberconst getDay = (str) => {&nbsp; &nbsp; return parseInt(str.slice(0, 2));};//create a dictionary of monthsconst monthDict = {&nbsp; &nbsp; Jan: 1,&nbsp; &nbsp; Feb: 2,&nbsp; &nbsp; Mar: 3,&nbsp; &nbsp; Apr: 4,&nbsp; &nbsp; May: 5,&nbsp; &nbsp; Jun: 6,&nbsp; &nbsp; Jul: 7,&nbsp; &nbsp; Aug: 8,&nbsp; &nbsp; Sep: 9,&nbsp; &nbsp; Oct: 10,&nbsp; &nbsp; Nov: 11,&nbsp; &nbsp; Dec: 12};//get the month value via dictionaryconst getMonth = (str) => {&nbsp; &nbsp; const month = str.slice(3, 6);&nbsp; &nbsp; return monthDict[month];};//get the year, convert to numberconst getYear = (str) => {&nbsp; &nbsp; return parseInt(str.slice(7));}//comparison helper functions//compare dayconst compareDay = (a, b) => {&nbsp; &nbsp; if (getDay(a) < getDay(b)) {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; } else if (getDay(a) === getDay(b)) {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; } else if (getDay(a) > getDay(b)) {&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }};//compare monthconst compareMonth = (a, b) => {&nbsp; &nbsp; if (getMonth(a) < getMonth(b)) {&nbsp; &nbsp; &nbsp; &nbsp; return -1&nbsp; &nbsp; } else if (getMonth(a) === getMonth(b)) {&nbsp; &nbsp; &nbsp; &nbsp; compareDay(a, b);&nbsp; &nbsp; } else if (getMonth(a) > getMonth(b)) {&nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; }};//compare yearconst compareYear = (a, b) => {&nbsp; &nbsp; if (getYear(a) < getYear(b)) {&nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; } else if (getYear(a) === getYear(b)) {&nbsp; &nbsp; &nbsp; &nbsp; compareMonth(a, b);&nbsp; &nbsp; } else if (getYear(a) > getYear(b)) {&nbsp; &nbsp; &nbsp; &nbsp; return 1&nbsp; &nbsp; }};//sort arrayconst sortedArray = cleanedDates.sort((a, b) => compareYear(a, b));console.log(sortedArray);

繁星点点滴滴

清理日期数组并且没有语法错误后,试试这个:// convert to datedates.map( el => new Date(el));// sort itdates.sort( (a,b) => a>b);
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答