-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsort.go
29 lines (25 loc) · 803 Bytes
/
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
package algo
// SortInterface is borrow from go lib sort.Interface
type SortInterface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
// StringSlice implements SortInterface
type StringSlice []string
func (p StringSlice) Len() int { return len(p) }
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// IsSorted reports if the given items is sorted
func IsSorted(items SortInterface) bool {
for i := 1; i < items.Len(); i++ {
if items.Less(i, i-1) {
return false
}
}
return true
}