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

039_go语言中的排序

时间:2018-04-20 14:34:26      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:print   变量   sar   func   import   []   pre   更新   pack   

代码演示:

package main

import "fmt"
import "sort"

func main() {
	strs := []string{"c", "a", "b"}
	sort.Strings(strs)
	fmt.Println("Strings: ", strs)

	ints := []int{7, 2, 4}
	sort.Ints(ints)
	fmt.Println("Ints: ", ints)

	s := sort.IntsAreSorted(ints)
	fmt.Println("Sorted: ", s)
}

  

代码运行结果:

Strings:  [a b c]
Ints:  [2 4 7]
Sorted:  true

  

代码解读:

  • go语言可以直接用sort包进行排序,本例中采用的是sort包中的内置排序功能
  • 排序是在原来的变量上更新的,不会返回一个新值
  • sort包也提供了一个检查是否排好序的功能,就是sort.IntsAreSorted,如果已经排好序的话,返回一个true

039_go语言中的排序

标签:print   变量   sar   func   import   []   pre   更新   pack   

原文地址:https://www.cnblogs.com/Joestar/p/8890360.html

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