Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Sample Analysis of sorting using sort in golang programming Development

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "golang programming development using sort sorting example analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The object of sort operation is usually a slice, which needs to satisfy three basic interfaces and can be indexed using integers.

/ / 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)}

Ex-1 sorts [] int from small to large

Package main import ("fmt"sort") type IntSlice [] intfunc (s IntSlice) Len () int {return len (s)} func (s IntSlice) Swap (I, j int) {s [I], s [j] = s [j], s [I]} func (s IntSlice) Less (I, j int) bool {return s [I] < s [j]} func main () {a: = [] int {4mt. 6} sort.Sort (IntSlice (a)) fmt.Println ("After sorted:", a)}

Ex-2 uses sort.Ints and sort.Strings

Golang defines IntSlice and StringSlice for the common [] int and [] string, respectively, and implements their respective sorting interfaces. Sort.Ints and sort.Strings can sort [] int and [] string directly, which is very convenient to use.

Package main import ("fmt"sort") func main () {a: = [] int {3,5,4,-1, 9, 11,-14} sort.Ints (a) fmt.Println (a) ss: = [] string {"surface", "ipad", "mac pro", "mac air", "think pad" "idea pad"} sort.Strings (ss) fmt.Println (ss) sort.Sort (sort.Reverse (sort.StringSlice (ss) fmt.Printf ("After reverse:% v\ n", ss)}

Ex-3 uses sort.Reverse for reverse sorting

If we want to reverse sort a sortable object, we can customize a type. But sort.Reverse saved you the code.

Package main import ("fmt"sort") func main () {a: = [] int {4, 3, 2, 5, 9, 8, 7} sort.Sort (sort.Reverse (sort.IntSlice (a) fmt.Println ("After reversed:", a)}

Ex-4 uses sort.Stable for stable sorting

Sort.Sort does not guarantee the stability of sorting. You can use sort.Stable if necessary

Package main import ("fmt"sort") type person struct {Name stringAge int} type personSlice [] person func (s personSlice) Len () int {return len (s)} func (s personSlice) Swap (I, j int) {s [I], s [j] = s [j], s [I]} func (s personSlice) Less (I, j int) bool {return s [I] .Age < s [j] .Age} func main () {a: = personSlice {{Name: "AAA", Age: 55 }, {Name: "BBB", Age: 22,}, {Name: "CCC", Age: 0,}, {Name: "DDD", Age: 22,}, {Name: "EEE", Age: 11,},} sort.Stable (a) fmt.Println (a)} "golang programming Development using sort sorting sample Analysis" ends here Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report