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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值