登录
首页 >  Golang >  Go教程

golang中按照结构体的某个字段排序实例代码

来源:脚本之家

时间:2022-12-24 21:55:22 317浏览 收藏

本篇文章主要是结合我之前面试的各种经历和实战开发中遇到的问题解决经验整理的,希望这篇《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 
<p>打印</p>
<blockquote><p>[{20 12} {10 11} {10 100} {10 110} {10 120}]</p></blockquote>
<h2>使用 sort.Stable 进行稳定排序</h2>
<p>sort.Sort 并不保证排序的稳定性。如果有需要, 可以使用 sort.Stable ,用法就是将sort.Sort 替换为 sort.Stable</p>
<p>附:go根据结构体中任意字段进行排序</p>
<h2>附:根据结构体中任意字段进行排序</h2>
<p>Sort()</p>
<p>Reverse()</p>
<p>Less(i,j int) bool</p>
<p>Len() int</p>
<p>Swap(i,j int)</p>
<pre class="brush:go;">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 func bynumber int this string const format="| %v |\t%v |\t%v |\t %v |\t %v |\t%v   |\n" fmt.println for k fmt.printf return main1 sts:="[]*Student{" b:="ByNumber(sts)" sort.sort type customsort struct s less bool len this.s this.less main c:="&customSort{" s: sts less: j if i.number j.number>j.Number
			}
			if i.Name!=j.Name{
				return i.Name<j.name return false package sort a type typically collection that satisfies sort.interface can be sorted by the routines in this package. methods require elements of enumerated an integer index. interface len is number collection. int less reports whether element with index i should before j. j bool swap swaps indexes and sort.sort fmt.println><h2>总结</h2>
<p>今天关于《golang中按照结构体的某个字段排序实例代码》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!</p></j.name></this>
声明:本文转载于:脚本之家 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>
评论列表