Golang对结构体进行排序
简介在PHP中,对二维数组排序还是非常简单的,但是在Golang中对二位数据排序显的就稍显麻烦,但是我们还是可以通过结构体来进行排序,本文主要记录Golang如何通过结构体的多个字段来进行排序。
1、方法一,通过实现sort.Interface接口来进行排序
这里直接上代码:
package main
import (
"fmt"
"sort"
)
type Student struct {
Name string //名称
IsOnline int64 //是否在线
Integral int64 //积分
Gold float64 //金币
}
type Students []*Student
// 实现sort.Interface接口取元素数量方法
func (c Students) Len() int {
return len(c)
}
// 实现sort.Interface接口比较元素方法
func (c Students) Less(i, j int) bool {
//先是否根据是否在线进行排序
if c[i].IsOnline != c[j].IsOnline {
return c[i].IsOnline > c[j].IsOnline
}
//在通过积分进行排序
if c[i].Integral != c[j].Integral {
return c[i].Integral > c[j].Integral
}
//最后通过金币排序
return c[i].Gold > c[j].Gold
}
// 实现sort.Interface接口交换元素方法
func (c Students) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func main() {
// 准备英雄列表
students := Students{
&Student{"吕布", 1, 12, 11.0},
&Student{"张飞", 0, 3, 12.1},
&Student{"关羽", 0, 6, 9.9},
&Student{"刘备", 1, 12, 0},
&Student{"诸葛亮", 1, 0, 1},
&Student{"曹操", 0, 1, 1000},
}
// 使用sort包进行排序
sort.Sort(students)
// 遍历英雄列表打印排序结果
for _, v := range students {
fmt.Printf("%+v\n", v)
}
}
结果:
>go run test.go
&{Name:吕布 IsOnline:1 Integral:12 Gold:11}
&{Name:刘备 IsOnline:1 Integral:12 Gold:0}
&{Name:诸葛亮 IsOnline:1 Integral:0 Gold:1}
&{Name:关羽 IsOnline:0 Integral:6 Gold:9.9}
&{Name:张飞 IsOnline:0 Integral:3 Gold:12.1}
&{Name:曹操 IsOnline:0 Integral:1 Gold:1000}
1、方法二,使用sort.Slice进行切片元素排序
package main import ( "fmt" "sort" ) type Student struct { Name string //名称 IsOnline int64 //是否在线 Integral int64 //积分 Gold float64 //金币 } type Students []*Student func main() { // 准备英雄列表 students := Students{ &Student{"吕布", 1, 12, 11.0}, &Student{"张飞", 0, 3, 12.1}, &Student{"关羽", 0, 6, 9.9}, &Student{"刘备", 1, 12, 0}, &Student{"诸葛亮", 1, 0, 1}, &Student{"曹操", 0, 1, 1000}, } // 使用sort包进行排序 sort.Slice(students, func(i, j int) bool { //先是否根据是否在线进行排序 if students[i].IsOnline != students[j].IsOnline { return students[i].IsOnline > students[j].IsOnline } //在通过积分进行排序 if students[i].Integral != students[j].Integral { return students[i].Integral > students[j].Integral } //最后通过金币排序 return students[i].Gold > students[j].Gold }) // 遍历英雄列表打印排序结果 for _, v := range students { fmt.Printf("%+v\n", v) } }
结果:
> go run test.go
&{Name:吕布 IsOnline:1 Integral:12 Gold:11}
&{Name:刘备 IsOnline:1 Integral:12 Gold:0}
&{Name:诸葛亮 IsOnline:1 Integral:0 Gold:1}
&{Name:关羽 IsOnline:0 Integral:6 Gold:9.9}
&{Name:张飞 IsOnline:0 Integral:3 Gold:12.1}
&{Name:曹操 IsOnline:0 Integral:1 Gold:1000}
在使用功能镜像的时候,需要对时区进行修改,那么就需要从两个方面来考虑,本文主要记录Dockerfile和docker-compose.yml设置时区。
《康熙王朝》是一部非常优秀的电视连续剧,陈道明演的康熙是我觉得最有帝王气魄,让人意犹未尽,本文主要记录一小段非常经典的对白。
以前配置了服务器的免密登录,但是后期重新装了操作系统,那么再次尝试却出现了WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!提示,那么我们需要将以前的公钥删掉
人脸识别技术如今已广泛应用于安全监控、身份验证、人机交互等多个领域。对于开发者而言,利用现有的库如face_recognition可以极大地简化人脸识别的开发过程。本文将详细介绍如何安装face_recognition库,并通过实例展示如何用它来实现一个基本的人脸比对服务。
快速生成表格
Electron页面跳转、浏览器打开链接和打开新窗口
在使用Git的过程中,不想每次都输入用户名和密码去拉取代码,所以就需要保存这些信息,那么既然有保存了,就必须有清除功能。
Docker编译镜像出现:fetch http://dl-cdn.alpinelinux.org/alpine/v3.12/main/x86_64/APKINDEX.tar.gz
ERROR: http://dl-cdn.alpinelinux.org/alpine/v3.12/main: temporary error (try again later)
WARNING: Ignoring APKINDEX.2c4ac24e.tar.gz: No such file or directory问题
在Mac电脑中,如何对Git的用户名和密码进行修改呢?起初不懂Mac,所以整了很久,本文将记录如何对这个进行操作,以便后期使用。