最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

golang中按照結(jié)構(gòu)體的某個字段排序?qū)嵗a

 更新時間:2022年05月29日 15:12:49   作者:raoxiaoya  
在任何編程語言中,關(guān)乎到數(shù)據(jù)的排序都會有對應(yīng)的策略,下面這篇文章主要給大家介紹了關(guān)于golang中按照結(jié)構(gòu)體的某個字段排序的相關(guān)資料,需要的朋友可以參考下

概述

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默認提供了三個類型,他們都實現(xiàn)了Interface:
Float64Slice
IntSlice
StringSlice

從大到小排序

方法1:先使用提供的從大到小排序,再翻轉(zhuǎn)

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]

方法二:自定義類型實現(xiàn)

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]
}

按照結(jié)構(gòu)體的某個字段排序

按年紀從大到小排序

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 進行穩(wěn)定排序

sort.Sort 并不保證排序的穩(wěn)定性。如果有需要, 可以使用 sort.Stable ,用法就是將sort.Sort 替換為 sort.Stable

附:go根據(jù)結(jié)構(gòu)體中任意字段進行排序

附:根據(jù)結(jié)構(gòu)體中任意字段進行排序

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學(xué)生信息表")
    fmt.Println(" 序號\t學(xué)號 \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("反轉(zhuǎn)")
	sort.Sort(sort.Reverse(b))  //反轉(zhuǎn)的用法
	fmt.Println(b)


	//為結(jié)構(gòu)體內(nèi)的每一個字段都綁定一個排序的外殼,這種操作顯然不是很聰明
    //這時候使用組合來解決這個問題
}


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 {    //可以指定多種排序規(guī)則
				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類型,還可以放結(jié)構(gòu)體類型,只要改類型 實現(xiàn) Sort接口
	fmt.Println(ByNumber(sts)) //單純的使用一下ByNumber中重寫是String()方法
}

總結(jié)

到此這篇關(guān)于golang中按照結(jié)構(gòu)體的某個字段排序的文章就介紹到這了,更多相關(guān)golang按字段排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 基于Go語言構(gòu)建RESTful API服務(wù)

    基于Go語言構(gòu)建RESTful API服務(wù)

    在實際開發(fā)項目中,你編寫的服務(wù)可以被其他服務(wù)使用,這樣就組成了微服務(wù)的架構(gòu);也可以被前端調(diào)用,這樣就可以前后端分離。那么,本文主要介紹什么是 RESTful API,以及 Go 語言是如何玩轉(zhuǎn) RESTful API 的
    2021-07-07
  • golang 比較浮點數(shù)的大小方式

    golang 比較浮點數(shù)的大小方式

    這篇文章主要介紹了golang 比較浮點數(shù)的大小方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • Go/C語言LeetCode題解997找到小鎮(zhèn)法官

    Go/C語言LeetCode題解997找到小鎮(zhèn)法官

    這篇文章主要為大家介紹了Go語言LeetCode題解997找到小鎮(zhèn)的法官示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • go語言中的Carbon庫時間處理技巧

    go語言中的Carbon庫時間處理技巧

    這篇文章主要介紹了go語言中的Carbon庫時間處理,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-02-02
  • golang連接MongoDB數(shù)據(jù)庫及數(shù)據(jù)庫操作指南

    golang連接MongoDB數(shù)據(jù)庫及數(shù)據(jù)庫操作指南

    MongoDB是Nosql中常用的一種數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于golang連接MongoDB數(shù)據(jù)庫及數(shù)據(jù)庫操作的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-09-09
  • 基于Go語言實現(xiàn)選擇排序算法及優(yōu)化

    基于Go語言實現(xiàn)選擇排序算法及優(yōu)化

    選擇排序是一種簡單的比較排序算法.這篇文章將利用Go語言實現(xiàn)冒泡排序算法,文中的示例代碼講解詳細,對學(xué)習(xí)Go語言有一定的幫助,需要的可以參考一下
    2022-12-12
  • Go語言中嵌入式緩存庫的用法詳解

    Go語言中嵌入式緩存庫的用法詳解

    Go?語言中有一些非常高效的嵌入式緩存庫,groupcache?和?bigcache?是兩個非常流行且高性能的庫,本文將詳細介紹一下二者的用法,有需要的小伙伴可以參考下
    2025-01-01
  • Go?語言的?:=的具體使用

    Go?語言的?:=的具體使用

    本文主要介紹了Go?語言的?:=的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 詳解Golang如何實現(xiàn)支持隨機刪除元素的堆

    詳解Golang如何實現(xiàn)支持隨機刪除元素的堆

    堆是一種非常常用的數(shù)據(jù)結(jié)構(gòu),它能夠支持在O(1)的時間復(fù)雜度獲取到最大值(或最小值)。本文主要介紹了如何實現(xiàn)支持O(log(n))隨機刪除元素的堆,需要的可以參考一下
    2022-09-09
  • golang?JSON序列化和反序列化示例詳解

    golang?JSON序列化和反序列化示例詳解

    通過使用Go語言的encoding/json包,你可以輕松地處理JSON數(shù)據(jù),無論是在客戶端應(yīng)用、服務(wù)器端應(yīng)用還是其他類型的Go程序中,這篇文章主要介紹了golang?JSON序列化和反序列化,需要的朋友可以參考下
    2024-04-04

最新評論

祁阳县| 库车县| 高邑县| 连江县| 泸西县| 奇台县| 汤原县| 当阳市| 呼图壁县| 陇南市| 丹巴县| 长泰县| 沙坪坝区| 克山县| 浠水县| 镶黄旗| 杨浦区| 徐水县| 焦作市| 叙永县| 连山| 修水县| 金塔县| 云阳县| 汶上县| 九龙县| 榆社县| 达尔| 高平市| 华蓥市| 改则县| 邹平县| 鹤峰县| 临武县| 吕梁市| 大余县| 凤城市| 龙州县| 襄樊市| 嫩江县| 女性|