Skip to content

Instantly share code, notes, and snippets.

@Lxxyx
Last active November 3, 2016 11:51
Show Gist options
  • Select an option

  • Save Lxxyx/8ff943887dffba2702462eaea1effce7 to your computer and use it in GitHub Desktop.

Select an option

Save Lxxyx/8ff943887dffba2702462eaea1effce7 to your computer and use it in GitHub Desktop.
SortSchedule
function sortWeek (schedules) {
schedules = schedules.map(s => sortDay(s))
}
function sortDay (schedule) {
const sorted = []
const hasSort = []
let base = schedule[0]
for (let i = 0; i < schedule.length; i++) {
if (base === null) {
hasSort.push(true)
sorted.push({
time: [i],
lesson: null
})
if (hasNext(schedule, i)) {
base = schedule[i+1]
continue
}
continue
}
if (hasSort[i] === true) {
continue
}
const lesson = {
time: [i],
lesson: schedule[i]
}
let next = i
while (hasNext(schedule, next) && isSame(base, schedule[next + 1])) {
next += 1
lesson.time.push(next)
hasSort.push(true)
}
hasSort.push(true)
sorted.push(lesson)
base = schedule[next + 1]
}
console.log(sorted)
}
function hasNext (arr, $index) {
if (arr[$index + 1] !== undefined) {
return true
}
return false
}
function isSame (curr, next) {
const { jxdd, jsmcmx, wxskbjmcmx, kcmc } = curr
if (next === null) {
return false
}
if (next.jxdd === jxdd && next.jsmcmx === jsmcmx && next.wxskbjmcmx === wxskbjmcmx && next.kcmc === kcmc) {
return true
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment