3 回答
TA贡献1797条经验 获得超6个赞
使用正则表达式,我们可以先提取总时间并在方法中以分钟为单位进行转换getTimeInMinutes,然后相应地进行比较。像这样:
function(a, b) {
function getTimeInMinutes(timestamp) {
const regex = /(?:(\d*?) days, )?(?:(\d*?) hours, )?(?:(\d*?) minutes)?/;
let [, days = 0, hours = 0, minutes = 0] = regex.exec(timestamp);
days = parseInt(days, 10);
hours = parseInt(hours, 10);
minutes = parseInt(minutes, 10);
const totalMinutes = (days * 24 * 60) + (hours * 60) + minutes;
return totalMinutes;
}
const totalMinutesInA = getTimeInMinutes(a);
const totalMinutesInB = getTimeInMinutes(b);
return totalMinutesInA - totalMinutesInB;
}
function compare(a, b) {
function getTimeInMinutes(timestamp) {
const regex = /(?:(\d*?) days, )?(?:(\d*?) hours, )?(?:(\d*?) minutes)?/;
let [, days = 0, hours = 0, minutes = 0] = regex.exec(timestamp);
days = parseInt(days, 10);
hours = parseInt(hours, 10);
minutes = parseInt(minutes, 10);
const totalMinutes = (days * 24 * 60) + (hours * 60) + minutes;
return totalMinutes;
}
const totalMinutesInA = getTimeInMinutes(a);
const totalMinutesInB = getTimeInMinutes(b);
console.log({totalMinutesInA, totalMinutesInB})
return totalMinutesInA - totalMinutesInB;
}
console.log('Test 1', compare('4 minutes', '3 minutes'));
console.log('Test 2', compare('24 minutes', '1 hours, 3 minutes'));
console.log('Test 2', compare('2 days, 24 minutes', '2 days, 24 minutes'))
TA贡献1909条经验 获得超7个赞
另一个相当冗长的选项是这样的: https ://jsfiddle.net/0k9asxeb/
var a = '16 days, 23 hours, 39 minutes';
var b = '39 minutes';
const getDayMinutes = (string) => {
if (!string.includes(' days')) {
return 0;
}
const days = string.split(' days')[0];
console.log(days);
return days * 24 * 60;
}
const getHoursMinutes = (string) => {
if (!string.includes (' hours')) {
return 0;
}
const hoursDirty = string.split(' hours')[0];
console.log(hoursDirty);
if (!hoursDirty.includes(',')) {
return hoursDirty;
}
const hours = hoursDirty.slice(hoursDirty.lastIndexOf(',') + 2, hoursDirty.length);
console.log(hours);
return hours * 60;
}
const getMinutes = (string) => {
if (!string.includes(' minutes')) {
return 0;
}
const minutesDirty = string.split(' minutes')[0];
console.log(minutesDirty);
if (!minutesDirty.includes(',')) {
return minutesDirty;
}
const minutes = minutesDirty.slice(minutesDirty.lastIndexOf(',') + 2, minutesDirty.length);
console.log(minutes);
return minutes;
}
const getTotalMinutesInString = (string) => {
const dayMinutes = getDayMinutes(string);
console.log('dayMinutes', dayMinutes);
const hoursMinutes = getHoursMinutes(string);
console.log('hoursMinutes', hoursMinutes);
const minutes = getMinutes(string);
console.log('minutes', minutes);
return parseInt(dayMinutes + hoursMinutes + minutes);
}
const totalMinutes = getTotalMinutesInString(b);
console.log(totalMinutes);
TA贡献1860条经验 获得超8个赞
创建一个将字符串转换为秒的函数。这是一个例子:
const toTimeConvertor = (timeEntry) => {
const string = timeEntry.replace(',', '');
switch(string) {
case 'day':
case 'days':
return 60 * 60 * 24;
case 'hour':
case 'hours':
return 60 * 60;
case 'minute':
case 'minutes':
return 60;
case 'second':
case 'seconds':
return 1;
default:
return 0;
}
};
const stringToSeconds = (timeString) => {
return [...timeString.split(',')].reduce((seconds, entry) => {
const values = entry.trim().split(' ');
const timeNumberString = values[0];
const timeConvertorString = values[1];
const timeNumber = Number.parseInt(timeNumberString);
const timeConvertor = toTimeConvertor(timeConvertorString)
const time = timeNumber * timeConvertor;
return seconds + time;
}, 0);
};
const myFilter = (a, b) => stringToSeconds(a) - stringToSeconds(b);
//Demo Output
const list = [
'16 days, 23 hours, 39 minutes',
'39 minutes',
'5 days, 36 minutes'
];
const sortedList = list.sort(myFilter);
console.log(list);
注意:如果输入是由用户生成的,您可能希望包括更多的生产检查。祝你好运。
添加回答
举报