-
Notifications
You must be signed in to change notification settings - Fork 3
/
sort.go
63 lines (51 loc) · 1.4 KB
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package teachingv1
import (
"encoding/json"
"sort"
)
type CoursesCustomLess struct {
*Courses
Less func(c CourseItems, i, j int) bool
}
func (x *CoursesCustomLess) MarshalJSON() ([]byte, error) {
list := CourseItems{}
for _, v := range x.Items {
list = append(list, v)
}
sort.Slice(list, func(i, j int) bool {
return x.Less(list, i, j)
})
return json.Marshal(list)
}
var courseItemsLess = DefaultCourseItemsLess
func SetCourseItemLess(less func(c CourseItems, i, j int) bool) {
courseItemsLess = less
}
var DefaultCourseItemsLess = func(c CourseItems, i, j int) bool {
return c[i].CourseID < c[j].CourseID
}
var CourseItemsLessByStartTime = func(c CourseItems, i, j int) bool {
return c[i].minStartTime() < c[j].minStartTime()
}
func (x *CourseItem) minStartTime() int64 {
if x.Schedule == nil || x.Schedule.Items == nil || len(x.Schedule.Items) == 0 {
return 0
}
min := int32(0)
for _, v := range x.Schedule.Items {
if len(v.Section) > 0 && (min == 0 || v.Section[0] < min) {
min = v.Section[0]
}
}
return int64(min)
}
var scheduleItemsLess = DefaultScheduleItemsLess
func SetScheduleItemLess(less func(c ScheduleItems, i, j int) bool) {
scheduleItemsLess = less
}
var DefaultScheduleItemsLess = func(c ScheduleItems, i, j int) bool {
return c[i].WeekDay < c[j].WeekDay
}
var ScheduleItemsLessByStartTime = func(c ScheduleItems, i, j int) bool {
return c[i].StartTime < c[j].StartTime
}