关于使用sort对定长数组进行排序--golang

今天写代码的时候相对一个定长数组进行排序,但是使用sort发现会报错

// A code block
sort.Sort(sort.IntSlice(a)
sort.Ints(a)
// An highlighted block
	var a [10]int
	//b := []int{3, 5, 4, -1, 9, 11, -14}
	for i := 0 ; i<10 ;i++{
		fmt.Scanf("%d",&a[i])
	}
	var tmp int = 10
	sort.Sort(sort.IntSlice(a)
	sort.Ints(a)

这样写是会报错的:cannot use a (type [10]int) as type []int in argument to sort.Ints
但是为什么会怎样呢,我查询有关golang的“sort”包的使用,都是一些类似于:对于b := []int{3, 5, 4, -1, 9, 11, -14}进行sort.Sort(sort.IntSlice(b)操作,但是定长声明的数组就不行。
解决方案:

sort.Sort(sort.IntSlice(a[:]))
sort.Ints(a[:])

这两种都可以,也可以通过:切片来进行部分排序。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值