<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] ## 語法 ``` func Float64s(a []float64) func Float64sAreSorted(a []float64) bool func Ints(a []int) func IntsAreSorted(a []int) bool func Strings(a []string) func StringsAreSorted(a []string) bool func IsSorted(data Interface) bool func Search(n int, f func(int) bool) int func SearchFloat64s(a []float64, x float64) int func SearchInts(a []int, x int) int func SearchStrings(a []string, x string) int func Slice(slice interface{}, less func(i, j int) bool) func SliceIsSorted(slice interface{}, less func(i, j int) bool) bool func SliceStable(slice interface{}, less func(i, j int) bool) func Sort(data Interface) func Stable(data Interface) ``` > Stable排序并保證排序的穩定性,相等元素的相對次序不變。 ## 示例 ### sort.Ints 排序 ``` ints := []int{5, 4, 2, 1, 1, 3, 4} sort.Ints(ints) fmt.Printf("%+v\n", ints) ``` ### sort.IntsAreSorted 是否排序 ``` sort.IntsAreSorted(ints) // true ``` ### sort.Reverse 倒排 ``` ints := []int{5, 4, 2, 1, 1, 3, 4} sort.Sort(sort.Reverse(sort.IntSlice(ints))) fmt.Printf("%+v\n", ints) // [5 4 4 3 2 1 1] ``` ### 結構體排序 #### 方式一: sort.Slice 直接使用 ``` people := []struct { Name string Age int }{ {"Gopher", 7}, {"Alice", 55}, {"Vera", 24}, {"Bob", 75}, } sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name }) fmt.Println("By name:", people) // By name: [{Alice 55} {Bob 75} {Gopher 7} {Vera 24}] ``` #### 方式二: sort.Slice 綁定結構體 <details> <summary>main.go</summary> ``` type People struct { Name string Age int } type Peoples []People func (p Peoples) sort() { sort.Slice(p, func(i, j int) bool { return p[i].Name < p[j].Name }) } func main() { people := Peoples{ {"Gopher", 7}, {"Alice", 55}, {"Vera", 24}, {"Bob", 75}, } people.sort() fmt.Printf("%+v\n", people) // [{Name:Vera Age:24} {Name:Gopher Age:7} {Name:Bob Age:75} {Name:Alice Age:55}] } ``` </details> <br /> #### 方式三: 實現 sort.Interface 接口 <details> <summary>main.go</summary> ``` package main import ( "fmt" "sort" ) //學生成績結構體 type StuScore struct { name string //姓名 score int //成績 } type StuScores []StuScore func (s StuScores) Len() int { return len(s) } func (s StuScores) Less(i, j int) bool { return s[i].score < s[j].score } func (s StuScores) Swap(i, j int) { s[i], s[j] = s[j], s[i] } func main() { stus := StuScores{ {"alan", 95}, {"hikerell", 91}, {"acmfly", 96}, {"leao", 90}, } sort.Sort(stus) //打印排序后的stus數據 fmt.Println("Sorted:\n\t", stus) } ``` </details> <br /> ### Search 對已排序進行二分查找 數組查找 ``` a := []int{1, 2, 3, 4, 5, 6} fmt.Printf("%+v\n", sort.SearchInts(a, 5)) // 4 ``` sort.Search 查詢 ``` num := []int{2, 3, 5, 7, 8, 22, 100} index := sort.Search(len(num), func(i int) bool { return num[i] > 21 }) fmt.Printf("%+v\n", num[index]) // 22 ``` 自定義結構查找 ``` people := []struct { Name string Age int }{ {"Gopher", 7}, {"Vera", 24}, {"Alice", 55}, {"Bob", 75}, } // 實際效果為滿足條件的第一個index index := sort.Search(len(people), func(i int) bool { // 等24 返回不正確, 需要使用 >=24 return people[i].Age >= 24 }) fmt.Printf("%+v\n", people[index]) // {Name:Vera Age:24} ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看