本网站(662p.com)打包出售,且带程序代码数据,662p.com域名,程序内核采用TP框架开发,需要联系扣扣:2360248666 /wx:lianweikj
精品域名一口价出售:1y1m.com(350元) ,6b7b.com(400元) , 5k5j.com(380元) , yayj.com(1800元), jiongzhun.com(1000元) , niuzen.com(2800元) , zennei.com(5000元)
需要联系扣扣:2360248666 /wx:lianweikj
golang中按照结构体的某个字段排序实例代码
chenguangming9 · 214浏览 · 发布于2022-05-30 +关注

在任何编程语言中,关乎到数据的排序都会有对应的策略,下面这篇文章主要给大家介绍了关于golang中按照结构体的某个字段排序的相关资料,需要的朋友可以参考下

概述

golang的sort包默认支持int, float64, string的从小大到排序:

int -> Ints(x []int)
float64 -> Float64s(x []float64)
string -> Strings(x []string)

同时它还提供了自定义的排序接口Interface,此接口保护三个方法。

type Interface interface {
    // Len is the number of elements in the collection.
    Len() int
    // Less reports whether the element with
    // index i should sort before the element with index j.
    Less(i, j int) bool
    // Swap swaps the elements with indexes i and j.
    Swap(i, j int)
}


golang默认提供了三个类型,他们都实现了Interface: 

Float64Slice
IntSlice
StringSlice

从大到小排序

方法1:先使用提供的从大到小排序,再翻转

arr := []float64{0.1, 0.5, 0.8, 0.4, 0.2}

sort.Sort(sort.Reverse(sort.Float64Slice(arr)))
fmt.Println(arr) // [0.8 0.5 0.4 0.2 0.1]


方法二:自定义类型实现 

type Float64SliceDecrement []float64
func (s Float64SliceDecrement) Len() int { return len(s) }
func (s Float64SliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Float64SliceDecrement) Less(i, j int) bool { return s[i] > s[j] }
func main() {
    arr := []float64{0.1, 0.5, 0.8, 0.4, 0.2}
     
    sort.Sort(Float64SliceDecrement(arr))
    fmt.Println(arr) // [0.8 0.5 0.4 0.2 0.1]
}


按照结构体的某个字段排序 

按年纪从大到小排序

type Persons struct {
    Age int
    Height int
}

type PersonsSliceDecrement []Persons
func (s PersonsSliceDecrement) Len() int { return len(s) }
func (s PersonsSliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s PersonsSliceDecrement) Less(i, j int) bool { return s[i].Age > s[j].Age }
func main() {
    arr1 := []Persons{
        Persons{10, 12},
        Persons{20, 12},
        Persons{9, 12},
        Persons{10, 12},
        Persons{11, 12},
    }
    sort.Sort(PersonsSliceDecrement(arr1))
    fmt.Println(arr1)
}


打印 

[{20 12} {11 12} {10 12} {10 12} {9 12}]

按年纪从大到小,如果年纪相等的,按身高从小到到

type Persons struct {
    Age int
    Height int
}
type PersonsSliceDecrement []Persons
func (s PersonsSliceDecrement) Len() int { return len(s) }
func (s PersonsSliceDecrement) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s PersonsSliceDecrement) Less(i, j int) bool {
    if s[i].Age > s[j].Age {
        return true
    }
    if s[i].Age == s[j].Age && s[i].Height < s[j].Height {
        return true
    }
    return false
}

func main() {
    arr1 := []Persons{
        Persons{10, 120},
        Persons{20, 12},
        Persons{10, 110},
        Persons{10, 11},
        Persons{10, 100},
    }
    sort.Sort(PersonsSliceDecrement(arr1))
    fmt.Println(arr1)
}


打印 

[{20 12} {10 11} {10 100} {10 110} {10 120}]

使用 sort.Stable 进行稳定排序

sort.Sort 并不保证排序的稳定性。如果有需要, 可以使用 sort.Stable ,用法就是将sort.Sort 替换为 sort.Stable

附:go根据结构体中任意字段进行排序

附:根据结构体中任意字段进行排序

Sort()

Reverse()

Less(i,j int) bool

Len() int

Swap(i,j int)

package main

import (
    "fmt"
    "sort"
)

type Student struct {
    Number   string
    Name     string
    Age      int
    IsWalker bool
    Weight   float32
}

type ByNumber []*Student

func (this ByNumber)Len() int  {
    return len(this)
}

func (this ByNumber)Less(i,j int) bool  {
    return this[i].Number<this[j].Number
}

func (this ByNumber)Swap(i,j int) {
    this[i],this[j] = this[j],this[i]
}

func (this ByNumber) String() string {
    const  format = "| %v |\t%v |\t%v |\t %v |\t %v |\t%v   |\n"
    fmt.Println("\t\t\t\t\t学生信息表")
    fmt.Println(" 序号\t学号 \t姓名\t   年龄\t  体重\t   是否走读")
    for k,v:=range this{
        fmt.Printf(format,k+1,v.Number,v.Name,v.Age,v.Weight,v.IsWalker)
    }
    return ""
}

func main1() {
    sts:=[]*Student{
        &Student{Number: "003",Name: "张三"},
        &Student{Number: "004",Name: "张四"},
        &Student{Number: "001",Name: "张一"},
        &Student{Number: "002",Name: "张二"},
        &Student{Number: "000",Name: "张零"},
    }
    b:=ByNumber(sts)
    sort.Sort(b)
    fmt.Println(b)
    fmt.Println("反转")
    sort.Sort(sort.Reverse(b))  //反转的用法
    fmt.Println(b)
    //为结构体内的每一个字段都绑定一个排序的外壳,这种操作显然不是很聪明
    //这时候使用组合来解决这个问题
}

type customSort struct {
    s []*Student
    less func(i,j *Student)  bool
}
func (this *customSort)Len() int {
    return len(this.s)
}

func (this *customSort)Swap(i,j int) {
    this.s[i],this.s[j] = this.s[j],this.s[i]
}

func (this *customSort)Less(i,j int) bool {
    return this.less(this.s[i],this.s[j])
}

func main()  {
    sts:=[]*Student{
        &Student{Number: "003",Name: "张三"},
        &Student{Number: "004",Name: "张四"},
        &Student{Number: "001",Name: "张一"},
        &Student{Number: "000",Name: "张二"},
        &Student{Number: "002",Name: "张二"},
    }

    c:=&customSort{
        s: sts,
        less: func(i, j *Student) bool {
            if i.Number != j.Number {    //可以指定多种排序规则
                return i.Number>j.Number
            }
            if i.Name!=j.Name{
                return i.Name<j.Name
            }
            return false
        },
    }
    /*
    package sort
    // A type, typically a collection, that satisfies sort.Interface can be
    // sorted by the routines in this package. The methods require that the
    // elements of the collection be enumerated by an integer index.
    type Interface interface {
        // Len is the number of elements in the collection.
        Len() int
        // Less reports whether the element with
        // index i should sort before the element with index j.
        Less(i, j int) bool
        // Swap swaps the elements with indexes i and j.
        Swap(i, j int)
    }
*/
    sort.Sort(c) //Sort方法中不只能放slice类型,还可以放结构体类型,只要改类型 实现 Sort接口
    fmt.Println(ByNumber(sts)) //单纯的使用一下ByNumber中重写是String()方法
}


 


相关推荐

PHP实现部分字符隐藏

沙雕mars · 1325浏览 · 2019-04-28 09:47:56
Java中ArrayList和LinkedList区别

kenrry1992 · 908浏览 · 2019-05-08 21:14:54
Tomcat 下载及安装配置

manongba · 970浏览 · 2019-05-13 21:03:56
JAVA变量介绍

manongba · 962浏览 · 2019-05-13 21:05:52
什么是SpringBoot

iamitnan · 1086浏览 · 2019-05-14 22:20:36
加载中

0评论

评论
分类专栏
小鸟云服务器
扫码进入手机网页