go排序示例
2016-05-05 16:28
302 查看
package main
import(
"fmt"
"sort"
"math/rand"
)
const(
male = iota
female
unknown
)
var genderStr []string=[]string{"male","female","unknown",}
type People struct{
Id int `ID`
Age int `Age`
Name string `Name`
Gender int `Gender`
}
func (p People) String() string{
return fmt.Sprintf("People %q of age %v with ID %v,Gender %s\n",p.Name,p.Age,p.Id,genderStr[p.Gender])
}
type Classmate []People
func (c Classmate) Len() int { return len(c) }
func (c Classmate) Less(i,j int) bool { return c[i].Age<c[j].Age }
func (c Classmate) Swap(i,j int) { c[i],c[j]=c[j],c[i] }
func main(){
cs:=make (Classmate,10,20)
for i:=0;i<len(cs); i++ {
cs[i].Id=i
cs[i].Age=rand.Intn(50)
cs[i].Name=fmt.Sprintf("cs_%v",i)
cs[i].Gender=rand.Intn(3)
}
fmt.Println(cs)
sort.Sort(cs)
fmt.Println(cs)
}
Screen output sample:
[People "cs_0" of age 31 with ID 0,Gender male
People "cs_1" of age 47 with ID 1,Gender unknown
People "cs_2" of age 31 with ID 2,Gender male
People "cs_3" of age 25 with ID 3,Gender unknown
People "cs_4" of age 6 with ID 4,Gender male
People "cs_5" of age 44 with ID 5,Gender female
People "cs_6" of age 12 with ID 6,Gender unknown
People "cs_7" of age 28 with ID 7,Gender unknown
People "cs_8" of age 11 with ID 8,Gender unknown
People "cs_9" of age 37 with ID 9,Gender unknown
]
[People "cs_4" of age 6 with ID 4,Gender male
People "cs_8" of age 11 with ID 8,Gender unknown
People "cs_6" of age 12 with ID 6,Gender unknown
People "cs_3" of age 25 with ID 3,Gender unknown
People "cs_7" of age 28 with ID 7,Gender unknown
People "cs_0" of age 31 with ID 0,Gender male
People "cs_2" of age 31 with ID 2,Gender male
People "cs_9" of age 37 with ID 9,Gender unknown
People "cs_5" of age 44 with ID 5,Gender female
People "cs_1" of age 47 with ID 1,Gender unknown
import(
"fmt"
"sort"
"math/rand"
)
const(
male = iota
female
unknown
)
var genderStr []string=[]string{"male","female","unknown",}
type People struct{
Id int `ID`
Age int `Age`
Name string `Name`
Gender int `Gender`
}
func (p People) String() string{
return fmt.Sprintf("People %q of age %v with ID %v,Gender %s\n",p.Name,p.Age,p.Id,genderStr[p.Gender])
}
type Classmate []People
func (c Classmate) Len() int { return len(c) }
func (c Classmate) Less(i,j int) bool { return c[i].Age<c[j].Age }
func (c Classmate) Swap(i,j int) { c[i],c[j]=c[j],c[i] }
func main(){
cs:=make (Classmate,10,20)
for i:=0;i<len(cs); i++ {
cs[i].Id=i
cs[i].Age=rand.Intn(50)
cs[i].Name=fmt.Sprintf("cs_%v",i)
cs[i].Gender=rand.Intn(3)
}
fmt.Println(cs)
sort.Sort(cs)
fmt.Println(cs)
}
Screen output sample:
[People "cs_0" of age 31 with ID 0,Gender male
People "cs_1" of age 47 with ID 1,Gender unknown
People "cs_2" of age 31 with ID 2,Gender male
People "cs_3" of age 25 with ID 3,Gender unknown
People "cs_4" of age 6 with ID 4,Gender male
People "cs_5" of age 44 with ID 5,Gender female
People "cs_6" of age 12 with ID 6,Gender unknown
People "cs_7" of age 28 with ID 7,Gender unknown
People "cs_8" of age 11 with ID 8,Gender unknown
People "cs_9" of age 37 with ID 9,Gender unknown
]
[People "cs_4" of age 6 with ID 4,Gender male
People "cs_8" of age 11 with ID 8,Gender unknown
People "cs_6" of age 12 with ID 6,Gender unknown
People "cs_3" of age 25 with ID 3,Gender unknown
People "cs_7" of age 28 with ID 7,Gender unknown
People "cs_0" of age 31 with ID 0,Gender male
People "cs_2" of age 31 with ID 2,Gender male
People "cs_9" of age 37 with ID 9,Gender unknown
People "cs_5" of age 44 with ID 5,Gender female
People "cs_1" of age 47 with ID 1,Gender unknown
相关文章推荐
- SQLSERVER 中GO的作用详解
- 在Go语言程序中使用gojson来解析JSON格式文件
- 举例详解Go语言中os库的常用函数用法
- Go语言中函数的参数传递与调用的基本方法
- 深入解析Go语言的io.ioutil标准库使用
- GO语言的IO方法实例小结
- Go语言的os包中常用函数初步归纳
- Go语言中数组的基本用法演示
- GO语言类型转换和类型断言实例分析
- 深入解析Go语言编程中的递归使用
- 初步解读Golang中的接口相关编写方法
- Go语言实现的最简单数独解法
- 详解Golang编程中的常量与变量
- Go实现比较时间大小
- 深入剖析Go语言编程中switch语句的使用
- 简单讲解Go程序中使用MySQL的方法
- Go语言的Windows下环境配置以及简单的程序结构讲解
- 在Go程序中实现服务器重启的方法
- Go语言流程控制之goto语句与无限循环
- 在Mac OS上安装Go语言编译器的方法