javascript - 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);
解决方案
你的语法不正确。其余的对我有用:)。,
当您const date
对 value进行操作时,您会丢失 a 22 Sep 1902
。}
当您执行 else if 时,在两个位置还有额外的内容。
修复它将使它工作:
//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);
推荐阅读
- java - 我的旧版 java web 应用程序 Servlet 只接受 HTTP 请求,我想将其更改为接受 HTTPS 请求。请建议
- java - ant run 命令的 JAVA_HOME 路径
- excel - 如何使用 VBA 在 Excel ListColumn 的 DataBodyRange 中从倒数第二个单元格中选择范围
- powershell - 等待安装过程向前移动 -Powershell
- javascript - 无法使用带有 GetElementByClass 的 span 类关闭模式框
- python - 如何在另一个文件夹中导入文件
- camunda - 如何以嵌入式形式访问流程变量
- python - python中的二维数组(非列表)
- javascript - 我无法使用 express res.cookie 发送 cookie
- javascript - 使用不同的并使用nodeJS一起排序