美文网首页
golang排序

golang排序

作者: fatshi | 来源:发表于2021-07-12 09:54 被阅读0次

【转】https://blog.csdn.net/u010983881/article/details/52460998

1. 升序排序

package main

import (
    "fmt"
    "sort"
)

func main() {
    intList := [] int {2, 4, 3, 5, 7, 6, 9, 8, 1, 0}
    float8List := [] float64 {4.2, 5.9, 12.3, 10.0, 50.4, 99.9, 31.4, 27.81828, 3.14}
    stringList := [] string {"a", "c", "b", "d", "f", "i", "z", "x", "w", "y"}

    sort.Ints(intList)
    sort.Float64s(float8List)
    sort.Strings(stringList)

    fmt.Printf("%v\n%v\n%v\n", intList, float8List, stringList)

}

2. 降序排序

package main

import (
    "fmt"
    "sort"
)

func main() {
    intList := [] int {2, 4, 3, 5, 7, 6, 9, 8, 1, 0}
    float8List := [] float64 {4.2, 5.9, 12.3, 10.0, 50.4, 99.9, 31.4, 27.81828, 3.14}
    stringList := [] string {"a", "c", "b", "d", "f", "i", "z", "x", "w", "y"}

    sort.Sort(sort.Reverse(sort.IntSlice(intList)))
    sort.Sort(sort.Reverse(sort.Float64Slice(float8List)))
    sort.Sort(sort.Reverse(sort.StringSlice(stringList)))

    fmt.Printf("%v\n%v\n%v\n", intList, float8List, stringList)
}

相关文章

网友评论

      本文标题:golang排序

      本文链接:https://www.haomeiwen.com/subject/ilrrpltx.html