1 回答

TA贡献1875条经验 获得超3个赞
就是把同个区间的时间分在一组吧,很简单,先排好序,再找出开始比前一个时间段的结尾要后的就行。
假设时间以 Number 方式存(距离 1 January 1970 00:00:00 UTC 的毫秒数)
时间段结构:
{
start: 1493125454502,
end: 1493125454516
}
function sortTime (times) {
if (times.length <= 1) { return times }
times = times.sort((a, b) => a.start !== b.start ? a.start - b.start : a.end - b.end)
let result = []
let beginIndex = 0
for (let i = 1; i < times.length; i += 1) {
if (times[i].start > times[i - 1].end) {
result.push(times.slice(beginIndex, i))
beginIndex = i
}
}
if (beginIndex !== times.length) {
result.push(times.slice(beginIndex, times.length))
}
return result
}
添加回答
举报