golang中使用 sort.Interface 实现复杂多级排序

举两个例子,说明 sort.Interface实现多级排序。

例子 1

学生成绩排序(先按成绩降序,成绩相同按姓名升序)

复制代码
  package main

  import (
      "fmt"
      "sort"
  )

  type Student struct {
      Name  string
      Score int
  }

  type ByScoreAndName []Student

  func (s ByScoreAndName) Len() int {
      return len(s)
  }

  func (s ByScoreAndName) Swap(i, j int) {
      s[i], s[j] = s[j], s[i]
  }

  func (s ByScoreAndName) Less(i, j int) bool {
      // 第一级:按分数降序(分数高的排前面)
      if s[i].Score != s[j].Score {
          return s[i].Score > s[j].Score
      }
      // 第二级:分数相同时,按姓名升序
      return s[i].Name < s[j].Name
  }

  func main() {
      students := []Student{
          {"Alice", 85},
          {"Bob", 90},
          {"Charlie", 85},
          {"David", 90},
      }

      sort.Sort(ByScoreAndName(students))

      for _, s := range students {
          fmt.Printf("%s: %d\n", s.Name, s.Score)
      }
      // 输出:
      // Bob: 90
      // David: 90
      // Alice: 85
      // Charlie: 85
  }

例子 2

员工排序(先按部门升序,部门相同按工资降序,工资相同按工号升序)

复制代码
  package main

  import (
      "fmt"
      "sort"
  )

  type Employee struct {
      ID         int
      Department string
      Salary     float64
  }

  type ByDeptSalaryID []Employee

  func (e ByDeptSalaryID) Len() int {
      return len(e)
  }

  func (e ByDeptSalaryID) Swap(i, j int) {
      e[i], e[j] = e[j], e[i]
  }

  func (e ByDeptSalaryID) Less(i, j int) bool {
      // 第一级:按部门升序
      if e[i].Department != e[j].Department {
          return e[i].Department < e[j].Department
      }
      // 第二级:部门相同时,按工资降序
      if e[i].Salary != e[j].Salary {
          return e[i].Salary > e[j].Salary
      }
      // 第三级:工资相同时,按工号升序
      return e[i].ID < e[j].ID
  }

  func main() {
      employees := []Employee{
          {103, "Engineering", 75000},
          {101, "Engineering", 80000},
          {102, "Engineering", 80000},
          {201, "Sales", 70000},
          {202, "Sales", 75000},
      }

      sort.Sort(ByDeptSalaryID(employees))

      for _, emp := range employees {
          fmt.Printf("ID: %d, Dept: %s, Salary: %.2f\n",
              emp.ID, emp.Department, emp.Salary)
      }
      // 输出:
      // ID: 101, Dept: Engineering, Salary: 80000.00
      // ID: 102, Dept: Engineering, Salary: 80000.00
      // ID: 103, Dept: Engineering, Salary: 75000.00
      // ID: 202, Dept: Sales, Salary: 75000.00
      // ID: 201, Dept: Sales, Salary: 70000.00
  }
相关推荐
菜鸟小芯1 天前
Qt Creator 集成开发环境下载安装
开发语言·qt
阿猿收手吧!1 天前
【C++】引用类型全解析:左值、右值与万能引用
开发语言·c++
「QT(C++)开发工程师」1 天前
C++ 策略模式
开发语言·c++·策略模式
iFeng的小屋1 天前
【2026最新当当网爬虫分享】用Python爬取千本日本相关图书,自动分析价格分布!
开发语言·爬虫·python
yugi9878381 天前
基于MATLAB的一键式EMD、EEMD、CEEMD和SSA信号去噪实现
开发语言·matlab·信号去噪
热爱编程的小刘1 天前
Lesson05&6 --- C&C++内存管理&模板初阶
开发语言·c++
Charlie_lll1 天前
力扣解题-[3379]转换数组
数据结构·后端·算法·leetcode
qq_12498707531 天前
基于Java Web的城市花园小区维修管理系统的设计与实现(源码+论文+部署+安装)
java·开发语言·前端·spring boot·spring·毕业设计·计算机毕业设计
VX:Fegn08951 天前
计算机毕业设计|基于springboot + vue云租车平台系统(源码+数据库+文档)
数据库·vue.js·spring boot·后端·课程设计
froginwe111 天前
Python 条件语句
开发语言