当前位置: 首页 > wzjs >正文

巴零网站建设广告免费推广网

巴零网站建设,广告免费推广网,网站怎么做百度推广,计算机是学什么内容的Go语言的 sort 模块提供了对切片和自定义数据结构的排序功能,支持基本类型排序、自定义排序规则、稳定排序和二分查找。以下是 sort 模块的核心方法及示例说明: 1. 基本类型排序 sort.Ints、sort.Float64s、sort.Strings 直接对基本类型的切片进行排序…

Go语言的 sort 模块提供了对切片和自定义数据结构的排序功能,支持基本类型排序、自定义排序规则、稳定排序和二分查找。以下是 sort 模块的核心方法及示例说明:


1. 基本类型排序

sort.Intssort.Float64ssort.Strings

直接对基本类型的切片进行排序。

package mainimport ("fmt""sort"
)func main() {// 整数切片排序ints := []int{3, 1, 4, 1, 5}sort.Ints(ints)fmt.Println(ints) // 输出: [1 1 3 4 5]// 浮点数切片排序floats := []float64{3.14, 2.71, 1.0}sort.Float64s(floats)fmt.Println(floats) // 输出: [1 2.71 3.14]// 字符串切片排序(字典序)strs := []string{"banana", "apple", "cherry"}sort.Strings(strs)fmt.Println(strs) // 输出: [apple banana cherry]
}

2. 自定义排序规则

sort.Slicesort.SliceStable

通过自定义比较函数对切片排序。

  • sort.Slice:不保证相等元素的顺序(非稳定排序)。
  • sort.SliceStable:保留相等元素的原始顺序(稳定排序)。
type Person struct {Name stringAge  int
}func main() {people := []Person{{"Alice", 30},{"Bob", 25},{"Charlie", 35},{"David", 25},}// 按年龄排序(非稳定)sort.Slice(people, func(i, j int) bool {return people[i].Age < people[j].Age})fmt.Println(people) // 输出顺序可能为 [Bob David Alice Charlie]// 按年龄排序(稳定)sort.SliceStable(people, func(i, j int) bool {return people[i].Age < people[j].Age})fmt.Println(people) // 输出顺序保持 [Bob David Alice Charlie]
}

3. 实现 sort.Interface 接口

通过实现 LenLessSwap 方法,自定义数据结构的排序逻辑。

type PersonList []Person// 实现 sort.Interface 接口
func (p PersonList) Len() int           { return len(p) }
func (p PersonList) Less(i, j int) bool { return p[i].Age < p[j].Age }
func (p PersonList) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }func main() {people := PersonList{{"Alice", 30},{"Bob", 25},{"Charlie", 35},}sort.Sort(people)fmt.Println(people) // 输出: [{Bob 25} {Alice 30} {Charlie 35}]
}

4. 逆序排序

sort.Reverse

反转排序逻辑,实现从大到小排序。

func main() {ints := []int{3, 1, 4, 1, 5}sort.Sort(sort.Reverse(sort.IntSlice(ints)))fmt.Println(ints) // 输出: [5 4 3 1 1]
}

5. 检查是否已排序

sort.IsSorted

验证切片是否已按顺序排序。

func main() {ints := []int{1, 2, 3}fmt.Println(sort.IsSorted(sort.IntSlice(ints))) // 输出: trueunsorted := []int{3, 1, 2}fmt.Println(sort.IsSorted(sort.IntSlice(unsorted))) // 输出: false
}

6. 二分查找

sort.Search

在已排序的切片中查找目标值,返回最小的满足条件的索引。

func main() {ints := []int{1, 3, 5, 7, 9}target := 5// 查找第一个 >= target 的索引index := sort.Search(len(ints), func(i int) bool {return ints[i] >= target})if index < len(ints) && ints[index] == target {fmt.Printf("找到 %d,索引为 %d\n", target, index) // 输出: 找到 5,索引为 2} else {fmt.Println("未找到")}
}

7. 自定义复杂排序

结合多个字段排序。

func main() {people := []Person{{"Alice", 30},{"Bob", 25},{"Charlie", 30},}// 先按年龄排序,年龄相同按名字字典序排序sort.Slice(people, func(i, j int) bool {if people[i].Age == people[j].Age {return people[i].Name < people[j].Name}return people[i].Age < people[j].Age})fmt.Println(people) // 输出: [{Bob 25} {Alice 30} {Charlie 30}]
}

总结

  • 核心方法
    • 基本排序IntsFloat64sStrings
    • 自定义排序SliceSliceStableSort(需实现 sort.Interface)。
    • 逆序排序Reverse
    • 检查排序IsSorted
    • 二分查找Search
  • 适用场景
    • 对基本类型切片快速排序。
    • 按自定义规则排序结构体切片。
    • 在有序数据中高效查找元素。
  • 注意事项
    • sort.Slice 更灵活,无需实现完整接口。
    • sort.Search 要求切片必须已按升序排列。

文章转载自:

http://dBg2muSd.tndhm.cn
http://Kznl3EkQ.tndhm.cn
http://QbGn4XWh.tndhm.cn
http://tyYkTIBY.tndhm.cn
http://IHjsJlgw.tndhm.cn
http://SoGlzrKH.tndhm.cn
http://FZ0lpFfI.tndhm.cn
http://txOvWcHb.tndhm.cn
http://97oRv5QM.tndhm.cn
http://AV41lVOn.tndhm.cn
http://eeNmmZEx.tndhm.cn
http://3KbE8QBo.tndhm.cn
http://P4sEu5fk.tndhm.cn
http://hoTmlnaO.tndhm.cn
http://mhIOLwP8.tndhm.cn
http://glznsxdl.tndhm.cn
http://ly7KrQMu.tndhm.cn
http://n8Fy22vy.tndhm.cn
http://qNQHduNQ.tndhm.cn
http://MjhDyVrs.tndhm.cn
http://ZLDoTKi2.tndhm.cn
http://v1emh27V.tndhm.cn
http://g58poGkC.tndhm.cn
http://KvYnA6vh.tndhm.cn
http://DdQqZSqp.tndhm.cn
http://baJ3oH4j.tndhm.cn
http://Zj5dnZO2.tndhm.cn
http://UhUpDVE6.tndhm.cn
http://fzdAB47B.tndhm.cn
http://eKladRsE.tndhm.cn
http://www.dtcms.com/wzjs/647840.html

相关文章:

  • 什么是网站推广环球影城可以寄存东西吗
  • 学生做网站的软件河源网站页面优化ppt
  • 简诉网站建设的基本流程wordpress教程书
  • 网站建设和信息工作会议wordpress干嘛用的
  • 双鸭山建设网站网站建设专家哪家好
  • 大学生实训网站建设心得为什么建设文化馆网站
  • 怎么做百度自己的网站广州做网站的
  • 网站宝的作用了解网页制作的基本知识
  • 酒泉如何做百度的网站主办单位性质与网站名称不符
  • 小说网站充值接口怎么做的企业邮箱怎么申请注册流程
  • 深圳官方网站建设哪里可以学短视频剪辑制作
  • 网站开发怎么切换多种语言单页面网站有哪些内容
  • 成都响应式网站郴州网站建设解决方案
  • 网站平台设计费用wordpress 404比较多
  • 国外公司在国内建网站wordpress链接失效
  • 福州企业网站建设做卖东西的网站多少钱
  • 如何做海外淘宝网站wordpress建站多少钱
  • 龙岗坪地网站建设能想到的域名都被注册了
  • 做金融的网站企业网站建设搭建
  • wordpress的网站怎样添加地图坐标商会网站建设方案书
  • 做网站域名需要在哪里备案wordpress悬浮代码
  • 微网站内容页模板wordpress 多大VPS
  • 毕节建设厅网站免费空间和域名
  • 高端定制网站是什么网站连接怎么做
  • 满分作文网站贵州企业网站建设策划
  • 如何制作自己的公司网站腾讯会议开始收费
  • 建设专业网站运营团队湖南人文科技学院官网教务系统
  • 学校校园网站 资源建设方案天津制作企业网站的
  • 市建设局网站顾客评价网站
  • 浦东建设网站阿里云cdn wordpress