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

该博客介绍了如何在Go语言中根据结构体的任意字段进行排序,包括创建自定义排序器ByNumber,实现Len、Less、Swap方法,以及通过customSort结构体实现多字段排序。示例代码展示了对Student结构体按Number字段排序及反转的过程,同时也展示了如何灵活地根据不同字段调整排序规则。
摘要由CSDN通过智能技术生成

排序

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

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()方法
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

metabit

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值