mirror of
https://github.com/unknwon/the-way-to-go_ZH_CN.git
synced 2025-08-11 20:02:56 +08:00
7.6.5
This commit is contained in:
@@ -66,4 +66,29 @@ s2 := string(c) // s2 == "cello"
|
||||
|
||||
## 7.6.5 字节数组对比函数
|
||||
|
||||
180
|
||||
下面的 `Compare` 函数会返回两个字节数组字典顺序的整数对比结果,即 `0 if a ==b, -1 if a < b, 1 if a > b`。
|
||||
|
||||
```go
|
||||
func Compare(a, b[]byte) int {
|
||||
for i:=0; i < len(a) && i < len(b); i++ {
|
||||
switch {
|
||||
case a[i] > b[i]:
|
||||
return 1
|
||||
case a[i] < b[i]:
|
||||
return -1
|
||||
}
|
||||
}
|
||||
// 数组的长度可能不同
|
||||
switch {
|
||||
case len(a) < len(b):
|
||||
return -1
|
||||
case len(a) > len(b):
|
||||
return 1
|
||||
}
|
||||
return 0 // 数组相等
|
||||
}
|
||||
```
|
||||
|
||||
## 7.6.6 搜索及排序切片和数组
|
||||
|
||||
181
|
||||
|
Reference in New Issue
Block a user