forked from continuum-samreen-shaikh/relay-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort.go
More file actions
38 lines (31 loc) · 823 Bytes
/
sort.go
File metadata and controls
38 lines (31 loc) · 823 Bytes
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
package relay
import (
"sort"
)
//SortBy : SortBy is function type of less function
type SortBy func(p1, p2 interface{}) bool
//Sort : Sort is a function on functiontype
func (sortBy SortBy) Sort(obj []interface{}) {
ps := &DataSorter{
obj: obj,
sortBy: sortBy,
}
sort.Sort(ps)
}
//DataSorter : DataSorter combines SortBy function and data to sort
type DataSorter struct {
obj []interface{}
sortBy func(p1, p2 interface{}) bool
}
//Len : Len gives length of data to sort
func (s *DataSorter) Len() int {
return len(s.obj)
}
//Swap : Swap is function to swap elements
func (s *DataSorter) Swap(i, j int) {
s.obj[i], s.obj[j] = s.obj[j], s.obj[i]
}
//Less : Less will be called by calling SortBy closure in the sorter
func (s *DataSorter) Less(i, j int) bool {
return s.sortBy(s.obj[i], s.obj[j])
}