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}
如果在使用Gin框架编写web应用程序中,如果里面包含了模板文件和静态文件,我们如何进行打包呢?本文主要记录该操作。
为了能让虚拟机能连接网络,并且能与宿主机能相互进行访问,所以需要多虚拟机几种网络模式进行学习,以便后期能快速的进行配置。
nodejs中使用npm和yarn,使用最新阿里云镜像 aliyun mirror,网上很多还是文章用的是下面这个地址~~yarn config set registry https://registry.npm.taobao.org~~
OpenCV-Python图像轮廓
默认情况下 pip 使用的是国外的镜像,在下载的时候速度非常慢,本文我们介绍使用国内源对pip进行加速。
快速生成表格
Electron页面跳转、浏览器打开链接和打开新窗口
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,所以整了很久,本文将记录如何对这个进行操作,以便后期使用。
在使用Git的过程中,不想每次都输入用户名和密码去拉取代码,所以就需要保存这些信息,那么既然有保存了,就必须有清除功能。