slice之排序,民间写法

stirlingx · 2020-04-03 15:41:17 · 542 次点击 · 大约8小时之前 开始浏览    置顶
这是一个创建于 2020-04-03 15:41:17 的主题,其中的信息可能已经有所发展或是发生改变。

int升序

package main

import (
    "github.com/liyue201/gostl/algorithm/sort"
    "github.com/liyue201/gostl/ds/slice"
    "fmt"
    "math/rand"
)

func main() {
    a := make([]int, 0)
    for i := 0; i < 10; i++ {
        a = append(a, rand.Int()%20)
    }
    sort.Sort(slice.IntSlice(a).Begin(), slice.IntSlice(a).End())
    fmt.Printf("%v\n", a)
}
[0 1 8 9 10 11 11 16 17 18]

int降序

package main

import (
    "github.com/liyue201/gostl/algorithm/sort"
    "github.com/liyue201/gostl/ds/slice"
    "fmt"
    "github.com/liyue201/gostl/utils/comparator"
    "math/rand"
)

func main() {
    a := make([]int, 0)
    for i := 0; i < 10; i++ {
        a = append(a, rand.Int()%20)
    }
    sort.Sort(slice.IntSlice(a).Begin(), slice.IntSlice(a).End(), comparator.Reverse(comparator.IntComparator))
    fmt.Printf("%v\n", a)
}
[18 17 16 11 11 10 9 8 1 0]

string升序

package main

import (
    "fmt"
    "github.com/liyue201/gostl/algorithm/sort"
    "github.com/liyue201/gostl/ds/slice"
    "math/rand"
    "strconv"
)

func main() {
    a := make([]string, 0)
    for i := 0; i < 10; i++ {
        a = append(a, "aa"+strconv.Itoa(rand.Int()%20))
    }
    sort.Sort(slice.StringSlice(a).Begin(), slice.StringSlice(a).End())
    fmt.Printf("%v\n", a)
}
[aa0 aa1 aa10 aa11 aa11 aa16 aa17 aa18 aa8 aa9]

奇偶分开再升序排序,偶数放左边,基数放右边

package main

import (
    "fmt"
    "github.com/liyue201/gostl/algorithm/sort"
    "github.com/liyue201/gostl/ds/slice"
    "math/rand"
)

func main() {
    a := make([]int, 0)
    for i := 0; i < 10; i++ {
        a = append(a, rand.Int()%20)
    }
    sort.Sort(slice.IntSlice(a).Begin(), slice.IntSlice(a).End(), func(a, b interface{}) int {
        va := a.(int)
        vb := b.(int)
        if va&1 < vb&1 {
            return -1
        }
        if va&1 > vb&1 {
            return 1
        }
        if va < vb {
            return -1
        }
        return 1
    })
    fmt.Printf("%v\n", a)
}
[0 8 10 16 18 1 9 11 11 17]

结构体排序

底层使用了反射,对性能要求高的场景不建议使用

package main

import (
    "fmt"
    "github.com/liyue201/gostl/algorithm/sort"
    "github.com/liyue201/gostl/ds/slice"
    "reflect"
)

type User struct {
    age  int
    name string
}

func main() {
    a := make([]*User, 0)
    a = append(a, &User{age: 2, name: "zzz"})
    a = append(a, &User{age: 5, name: "nnn"})
    a = append(a, &User{age: 2, name: "aaa"})

    sw := slice.NewSliceWrapper(a, reflect.TypeOf(&User{})) 

    // 按age排序,再按name排序
    sort.Sort(sw.Begin(), sw.End(), func(a, b interface{}) int {
        ua := a.(*User)
        ub := b.(*User)
        if ua.age < ub.age {
            return -1
        }
        if ua.age > ub.age {
            return 1
        }
        if ua.name < ub.name {
            return -1
        }
        if ua.name > ub.name {
            return 1
        }
        return 0
    })

    for i := range a {
        fmt.Printf("%+v\n", a[i])
    }
}
&{age:2 name:aaa}
&{age:2 name:zzz}
&{age:5 name:nnn}

有疑问加站长微信联系(非本文作者)

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

542 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传