Skip to content

Sort slice of struct by specified field #12

@kevinyan815

Description

@kevinyan815
package main

import (
	"fmt"
	"strings"
	"sort"
)

func main() {
    family := []struct {
        Name string
        Age  int
    }{
        {"Alice", 23},
        {"David", 2},
        {"Eve", 2},
        {"Bob", 25},
    }

    // 用 age 排序,年龄相等的元素保持原始顺序
    sort.SliceStable(family, func(i, j int) bool {
        return family[i].Age < family[j].Age
    })
    fmt.Println(family) // [{David 2} {Eve 2} {Alice 23} {Bob 25}]

    //下面实现排序order by age asc, name desc,如果 age 和 name 都相等则保持原始排序
    sort.SliceStable(family, func(i, j int) bool {
        if family[i].Age != family[j].Age {
            return family[i].Age < family[j].Age
        }
        return strings.Compare(family[i].Name, family[j].Name) == 1
    })

    fmt.Println(family) // [{Eve 2} {David 2} {Alice 23} {Bob 25}]
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions