<p><img src="https://img.php.cn/upload/article/000/000/164/170740497113136.jpg" alt="在 GoLang 中对自定义结构体数组进行排序"></p>
<p>In GoLang, sorting custom structure arrays is a common requirement. By comparing and exchanging elements in the array, we can sort the structure array according to specific rules. During the sorting process, we can use different sorting algorithms, such as bubble sort, insertion sort, or quick sort, etc. No matter which algorithm is used, we can compare based on a certain field of the structure to achieve the sorting operation. In this article, we will introduce how to sort a custom structure array in GoLang, as well as some common sorting tips and considerations. </p>
<h2 class="daan">Question content</h2>
<p>How to use golang to sort a custom structure array. </p>
<p>My code is: </p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">package main
import "fmt"
type ticketdistribution struct {
label string
ticketvolume int64
}
type ticketdistributionresponse struct {
leveldistribution []*ticketdistribution
}
func main() {
var response ticketdistributionresponse
response.leveldistribution = append(response.leveldistribution, &ticketdistribution{label: "john", ticketvolume: 3})
response.leveldistribution = append(response.leveldistribution, &ticketdistribution{label: "bill", ticketvolume: 7})
response.leveldistribution = append(response.leveldistribution, &ticketdistribution{label: "sam", ticketvolume: 4})
for _, val := range response.leveldistribution {
fmt.println(*val)
}
}</pre><div class="contentsignin">Copy after login</div></div>
<p>This prints the output as </p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">{john 3}
{bill 7}
{sam 4}</pre><div class="contentsignin">Copy after login</div></div>
<p>I want to sort the <strong>response</strong> objects in descending order by <strong>ticketvolume</strong> value. </p>
<p>After sorting, the response object should look like this: </p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">{Bill 7}
{Sam 4}
{John 3}</pre><div class="contentsignin">Copy after login</div></div></p><h2 class="daan">Workaround</h2><p>You can use <a href="https://www.php.cn/link/ad0efad9dd0abaec4b8f9aaa489ec2f1" rel="nofollow noreferrer"><code>sort.slice</code></a> to achieve this. It requires your slicing and sorting functions.
The sort function itself takes two indices and returns true if the item <strong> on the left is less than the item </strong> on the right. </p>
<p>This way you can sort by your own custom criteria. </p>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false;">package main
import (
"fmt"
"sort"
)
type TicketDistribution struct {
Label string
TicketVolume int64
}
type TicketDistributionResponse struct {
LevelDistribution []*TicketDistribution
}
func main() {
var response TicketDistributionResponse
response.LevelDistribution = append(response.LevelDistribution, &TicketDistribution{Label: "John", TicketVolume: 3})
response.LevelDistribution = append(response.LevelDistribution, &TicketDistribution{Label: "Bill", TicketVolume: 7})
response.LevelDistribution = append(response.LevelDistribution, &TicketDistribution{Label: "Sam", TicketVolume: 4})
sort.Slice(response.LevelDistribution, func(i, j int) bool {
a := response.LevelDistribution[i]
b := response.LevelDistribution[j]
return a.TicketVolume > b.TicketVolume
})
for _, val := range response.LevelDistribution {
fmt.Println(*val)
}
}</pre><div class="contentsignin">Copy after login</div></div>
<p>Use <code>></code> in the comparison function to sort the slices in descending order, for ascending order you can use <code><</code>. </p>
The above is the detailed content of Sorting custom structure array in GoLang. For more information, please follow other related articles on the PHP Chinese website!