1.根据结构体里的map里的属性进行排序
2.根据不同属性进行多重排序
package main
import (
"github.com/pantsing/log"
"sort"
)
type Animal struct {
allPerson []*Person
count int32
}
type Person struct {
name string
age int
mp map[string]string
}
//将[]Person和比较的准则cmp封装在了一起,形成了PersonWrapper 函数
type PersonWrapper struct {
person []*Person
by func(p, q * Person) bool
}
type SortBy func(p, q *Person) bool
func (pw PersonWrapper) Len() int { // 重写 Len() 方法
return len(pw.person)
}
func (pw PersonWrapper) Swap(i, j int){ // 重写 Swap() 方法
pw.person[i], pw.person[j] = pw.person[j], pw.person[i]
}
func (pw PersonWrapper) Less(i, j int) bool { // 重写 Less() 方法
return pw.by(pw.person[i], pw.person[j])
}
func main() {
mp1 := map[string]string{"a":"100","b":"abc"}
mp2 := map[string]string{"a":"20","b":"acc"}
mp3 := map[string]string{"a":"1001","b":"aac"}
person1 := &Person{
name:"wangwu",
age:18,
mp:mp1,
}
person2 := &Person{
name:"lisi",
age:18,
mp:mp2,
}
person3 := &Person{
name:"zhangsan",
age:38,
mp:mp3,
}
var persons []*Person
persons = append(persons, person1)
persons = append(persons, person2)
persons = append(persons, person3)
var animal *Animal = new(Animal)
animal.count = 3
log.Print(animal.count)
animal.allPerson = persons
Print(animal.allPerson)
sort.Sort(PersonWrapper{animal.allPerson, func (p, q *Person) bool {
/*
将结构体中map的字符串类型属性转换成整型类型进行排序,前提字符串为数字形式,否则报错
p1,_:= strconv.Atoi(p.mp["a"]) //将字符串转换为整数进行排序
p2,_:= strconv.Atoi(q.mp["a"])
return p1 < p2 // 递增排序
//return p1 > p2 // 递减排序
*/
/*
多重排序
按照年龄从小到大排序,如果年龄相同,则按照姓名进行字典序排序
if p.age == q.age{
return p.name < q.name
}
return p.age < q.age
*/
//按照姓名进行字典序排序
return p.name < q.name
}})
log.Println("sort after")
Print(animal.allPerson)
}
//打印结构体进行查看
func Print(p []*Person){
for k,v := range p{
log.Println(k, " ", v)
}
}
3,自定义多级排序,根据自定义多级排序的级数和属性,递归实现排序规则
package main
import (
"github.com/pantsing/log"
"sort"
)
type Person struct {
mp map[string]string
}
//将[]Person和比较的准则cmp封装在了一起,形成了PersonWrapper 函数
type PersonWrapper struct {
person []*Person
by func(p, q * Person) bool
}
type SortBy func(p, q *Person) bool
func (pw PersonWrapper) Len() int { // 重写 Len() 方法
return len(pw.person)
}
func (pw PersonWrapper) Swap(i, j int){ // 重写 Swap() 方法
pw.person[i], pw.person[j] = pw.person[j], pw.person[i]
}
func (pw PersonWrapper) Less(i, j int) bool { // 重写 Less() 方法
return pw.by(pw.person[i], pw.person[j])
}
func dfsSort(p,q *Person, index int, sortType []string) bool{
s := sortType[index]
//如果是最后一级,则无需递归,直接排序
if index == len(sortType) -1{
return p.mp[s] < q.mp[s]
}
//如果排序的属性相同,且存在下一级排序,则继续递归排序
if p.mp[s] == q.mp[s] && index+1 < len(sortType){
return dfsSort(p,q,index+1,sortType)
}else{//否则此级的属性进行排序
return p.mp[s] < q.mp[s]
}
}
func SortRule(persons []*Person){
sort.Sort(PersonWrapper{persons, func (p, q *Person) bool {
//自定义多级排序规则,age第一,sex第二,name第三,age相同则排sex,sex相同则排name
sortType := []string{"age", "sex","name"}
return dfsSort(p,q,0,sortType)
}})
}
var persons []*Person
func main() {
Print(persons)
SortRule(persons)
log.Println("sort after")
Print(persons)
}
//打印结构体进行查看
func Print(p []*Person){
for k,v := range p{
log.Println(k, " ", v)
}
}
//初始化排序数据
func init() {
mp1 := map[string]string{
"name":"wangwu",
"age":"18",
"sex":"boy",
}
mp2 := map[string]string{
"name":"lisi",
"age":"18",
"sex":"boy",
}
mp3 := map[string]string{
"name":"zhangsan",
"age":"38",
"sex":"girl",
}
mp4 := map[string]string{
"name":"zhangsan1",
"age":"38",
"sex":"girl",
}
mp5 := map[string]string{
"name":"lisi",
"age":"17",
"sex":"boy",
}
mp6 := map[string]string{
"name":"lisi",
"age":"17",
"sex":"girl",
}
mp7 := map[string]string{
"name":"lisi",
"age":"18",
"sex":"girl",
}
mp8 := map[string]string{
"name":"lisi",
"age":"18",
"sex":"boy",
}
mp9 := map[string]string{
"name":"lisi",
"age":"19",
"sex":"boy",
}
person1 := &Person{
mp1,
}
person2 := &Person{
mp2,
}
person3 := &Person{
mp3,
}
person4 := &Person{
mp4,
}
person5 := &Person{
mp5,
}
person6 := &Person{
mp6,
}
person7 := &Person{
mp7,
}
person8 := &Person{
mp8,
}
person9 := &Person{
mp9,
}
persons = append(persons, person1)
persons = append(persons, person2)
persons = append(persons, person3)
persons = append(persons, person4)
persons = append(persons, person5)
persons = append(persons, person6)
persons = append(persons, person7)
persons = append(persons, person8)
persons = append(persons, person9)
}