侃侃无极
您需要一个比较器函数,该函数可以根据您定义的排序规则进行正确比较。要对一段字符串进行排序,首先按长度,然后按字母顺序,您应该这样做:strs := []string{ "Zulu" , "Yankee" , "X-Ray" , "Whiskey" , "Victor" , "Ulysses" , "Tango" , "Sierra" , "Romeo" , "Quebec" , "Poppa" ,"Oscar" , "Nancy" , "Mike" , "Lima" , "Kilo" , "Juliet" , "India" , "Hotel" , "Golf" , "Foxtrot" , "Echo" , "Delta" , "Charlie" , "Bravo" , "Alpha" , } byLengthThenAlphabetically := func(i int, j int) bool { x := strs[i] y := strs[j] deltaLength := len(x) - len(y) return deltaLength < 0 || (deltaLength == 0 && x < y) } sort.Slice(strs, byLengthThenAlphabetically )在https://play.golang.org/p/nL2bTDlWM49的 Go Playground 中尝试一下