-
Notifications
You must be signed in to change notification settings - Fork 4
/
sort2.go
executable file
·64 lines (53 loc) · 1.42 KB
/
sort2.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
64
//++++++++++++++++++++++++++++++++++++++++
// 《Go语言高级开发与实战》源码
//++++++++++++++++++++++++++++++++++++++++
// Author:廖显东(ShirDon)
// Blog:https://www.shirdon.com/
// 知乎:https://www.zhihu.com/people/shirdonl
// 公众号:源码大数据
// 仓库地址:https://gitee.com/shirdonl/goAdvanced
// 仓库地址:https://github.com/shirdonl/goAdvanced
//++++++++++++++++++++++++++++++++++++++++
package array
import (
"fmt"
"sort"
)
//按指定规则对numArray进行排序
func ArraySort(numArray [][]int, firstIndex int) [][]int {
//检查
if len(numArray) <= 1 {
return numArray
}
if firstIndex < 0 || firstIndex > len(numArray[0])-1 {
fmt.Println("Warning: Param firstIndex should between 0 and len(numArray)-1. The original array is returned.")
return numArray
}
//排序
mIntArray := &IntArray{numArray, firstIndex}
sort.Sort(mIntArray)
return mIntArray.mArr
}
type IntArray struct {
mArr [][]int
firstIndex int
}
//IntArray实现sort.Interface接口
func (arr *IntArray) Len() int {
return len(arr.mArr)
}
func (arr *IntArray) Swap(i, j int) {
arr.mArr[i], arr.mArr[j] = arr.mArr[j], arr.mArr[i]
}
func (arr *IntArray) Less(i, j int) bool {
arr1 := arr.mArr[i]
arr2 := arr.mArr[j]
for index := arr.firstIndex; index < len(arr1); index++ {
if arr1[index] < arr2[index] {
return true
} else if arr1[index] > arr2[index] {
return false
}
}
return i < j
}