本篇文章介绍如何在 Go 语言中对结构切片进行排序。


GoLang 对结构体切片进行排序

sort.Slicesort.SliceStable

这些方法的语法是:

//sort.Slice
func Slice(x StructSlice, less func(i, j int) bool)

//sort.SliceStable
func Slice(x StructSlice, less func(i, j int) bool)

其中 x 是结构的切片,切片将根据 less 函数进行排序。 让我们尝试实现一些示例来了解如何在 GoLang 中对结构切片进行排序。


按字段对结构切片进行排序

按字段对结构切片进行排序是对结构切片进行排序的基本示例。 我们根据字段将结构切片的一个成员的值与同一结构切片的另一个成员的值进行比较,让我们看一个例子。

package main

import (
    "fmt"
    "sort"
)

type Employee struct {
    Name   string
    Salary int
}

func main() {
    // Sort in Ascending order
    employees := []Employee{
        {Name: "John", Salary: 1500},
        {Name: "Joe", Salary: 3000},
        {Name: "Jack", Salary: 3400},
    }

    sort.Slice(employees, func(i, j int) bool {
        return employees[i].Salary < employees[j].Salary
    })

    fmt.Println(employees)

    // Sort in Descending order
    employees1 := []Employee{
        {Name: "John", Salary: 1500},
        {Name: "Joe", Salary: 3000},
        {Name: "Jack", Salary: 3400},
    }

    sort.Slice(employees1, func(i, j int) bool {
        return employees1[i].Salary > employees1[j].Salary
    })

    fmt.Println(employees1)
}

上面的代码包含一个 Employee 结构体,它有两个字段 Name 和 Salary,然后它创建一个结构体切片并使用 Slice 方法根据字段 Salary 对其进行升序排序,然后将另一个切片进行降序排序。

查看输出:

[{John 1500} {Joe 3000} {Jack 3400}]
[{Jack 3400} {Joe 3000} {John 1500}]
sort.SliceStablesort.SliceStable
package main

import (
    "fmt"
    "sort"
)

type Employee struct {
    Name   string
    Salary int
}

func main() {
    // Sort in Ascending order
    employees := []Employee{
        {"John", 1500},
        {"Joe", 3000},
        {"Jack", 3400},
    }

    sort.SliceStable(employees, func(i, j int) bool {
        return employees[i].Salary < employees[j].Salary
    })

    fmt.Println(employees)

    // Sort in Descending order
    employees1 := []Employee{
        {"John", 1500},
        {"Joe", 3000},
        {"Jack", 3400},
    }

    sort.SliceStable(employees1, func(i, j int) bool {
        return employees1[i].Salary > employees1[j].Salary
    })

    fmt.Println(employees1)
}

上面的代码将类似地根据结构切片中没有名称和值的键值对的字段对结构切片进行排序。

查看输出:

[{John 1500} {Joe 3000} {Jack 3400}]
[{Jack 3400} {Joe 3000} {John 1500}]

按多个字段对结构切片进行排序

也可以按多个字段对结构切片进行排序。 我们必须扩展 less 函数; 让我们举个例子。

package main

import (
    "fmt"
    "sort"
)

type Employee struct {
    Name     string
    Position string
}

func main() {
    // Sort in Descending order
    employees := []Employee{
        {"Michael", "Developer"},
        {"Jack", "Manager"},
        {"Joe", "CEO"},
        {"Leonard", "Intern"},
        {"Sheldon", "Developer"},
    }

    sort.SliceStable(employees, func(i, j int) bool {
        if employees[i].Position != employees[j].Position {
            return employees[i].Position < employees[j].Position
        }

        return employees[i].Name < employees[j].Name
    })

    fmt.Println(employees)

    // Sort in Ascending order
    employees1 := []Employee{
        {"Michael", "Developer"},
        {"Jack", "Manager"},
        {"Joe", "CEO"},
        {"Leonard", "Intern"},
        {"Sheldon", "Developer"},
    }

    sort.SliceStable(employees1, func(i, j int) bool {
        if employees1[i].Position != employees1[j].Position {
            return employees1[i].Position > employees1[j].Position
        }

        return employees1[i].Name > employees1[j].Name
    })

    fmt.Println(employees1)
}

上面的代码将根据 Name 和 Position 进行排序,如果两个员工的职位相同,则将根据 Name 字段进行排序。 结构的切片将按降序和升序排序。

查看输出:

[{Joe CEO} {Michael Developer} {Sheldon Developer} {Leonard Intern} {Jack Manager}]
[{Jack Manager} {Leonard Intern} {Sheldon Developer} {Michael Developer} {Joe CEO}]