码迷,mamicode.com
首页 > 编程语言 > 详细

go--sort对数组进行排序

时间:2018-06-29 00:02:50      阅读:2866      评论:0      收藏:0      [点我收藏+]

标签:str   func   pac   直接   oat   一个   sea   返回   切片   

package main

import (
	"sort"
	"fmt"
)

func main(){
	// 1.对整形数组排序
	s1 := [...]int{1,3,43,3,5,6,32,56,7,8,53,}
	sort.Ints(s1[:])  //切片本质上是对底层数组的一个view,不能直接修改view,通过视图来修改
	fmt.Println(s1) //[1 3 3 5 6 7 8 32 43 53 56]

	// 2.对字符串类型数组进行排序
	s2:=[...]string{"mashiro","satori","hatsunemiku"}
	sort.Strings(s2[:])
	fmt.Println(s2) //[hatsunemiku mashiro satori]

	// 3.对浮点数类型数组进行排序
	s3:=[...]float64{12.3,31.31,312.2,1.1,43,2}
	sort.Float64s(s3[:])
	fmt.Println(s3) //[1.1 2 12.3 31.31 43 312.2]

	// 4.查找一个int在数组中的索引
	s4:=[...]int{2,3,452,1,3,4,5,67}
	index:=sort.SearchInts(s4[:],3)
	fmt.Println(index) //如果有多个,只返回第一个

	// 5.查找一个string在数组中的位置
	s5:=[...]string{"mashiro","satori","hatsunemiku"}
	index1 := sort.SearchStrings(s5[:],"mashiro")
	fmt.Println(index1) //0
}

  

go--sort对数组进行排序

标签:str   func   pac   直接   oat   一个   sea   返回   切片   

原文地址:https://www.cnblogs.com/traditional/p/9241190.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!