維基百科關(guān)于字典順序的例子:
Given two different sequences of the same length, a1a2...ak and b1b2...bk, the first one is smaller than the second one for the lexicographical order, if ai<bi (for the order of A), for the first i where ai and bi differ.
簡(jiǎn)單說(shuō)就是給定兩個(gè)串,分別從每個(gè)串的頭開(kāi)始依次比較串的元素的大小,當(dāng)?shù)谝粋€(gè)不同的元素出現(xiàn)時(shí)(ai != bi)比較就結(jié)束了,且ai與bi的比較結(jié)果作為串比較的結(jié)果。
此外,短串跟長(zhǎng)串相比時(shí),短串不足的位置會(huì)作為空元素處理,且空元素比其它非空元素小。
package main
import (
"bytes"
"fmt"
)
func main() {
sa := []byte{2, 3}
sb := []byte{12, 3}
sc := []byte{3}
sd := []byte{2,1,5}
fmt.Println("sa.comp(sb)", bytes.Compare(sa, sb))
fmt.Println("sa.comp(sc)", bytes.Compare(sa, sc))
fmt.Println("sa.comp(sd)", bytes.Compare(sa, sd))
}
點(diǎn)擊運(yùn)行查看結(jié)果