如何在Go语言中按字典序(词典顺序)排序元素?
在本教程中,我们将编写一个Go语言程序,按字典序对元素进行排序。本教程将包含三种不同的方法。为了进行排序,我们需要比较两个字符串,为此,我们将使用<运算符,它将返回一个布尔值。如果左侧的值在字典序上大于右侧的值,则返回false,否则返回true。
例如:
Tutorial < Point - 运算符将返回true。
C++ < Golang - 运算符将返回false。
算法
步骤1 − 创建并初始化一个字符串类型的切片。
步骤2 − 运行嵌套的for循环,在循环内,我们从左侧选择每个索引,并与大于该索引的索引进行比较,并将剩余元素中的最小元素存储起来。
步骤3 − 打印排序后的元素。
示例
在这个例子中,我们将对函数内的元素进行字典序排序。
package main // fmt package provides the function to print anything import "fmt" func main() { // creating a slice of string type and storing the element stringSlice := [5]string{"Tutorial", "Point", "Java", "C++", "Golang"} var temp string fmt.Println("Program to sort the element in lexicographical order within the function.") fmt.Println() fmt.Println("Elements before sorting in lexicographical order.") // printing the elements before sorting them in lexicographical order for i := 0; i < 5; i++ { fmt.Print(stringSlice[i], " ") } fmt.Println() // running the nested loops and picking one index at a time // and find the right element in lexicographical order // on that index for i := 0; i < 5; i++ { for j := i + 1; j < 5; j++ { // comparing the strings at index i and index j // if string at index i is greater in lexicographical // order than doing the swap of both elements if stringSlice[i] > stringSlice[j] { temp = stringSlice[i] stringSlice[i] = stringSlice[j] stringSlice[j] = temp } } } fmt.Println() fmt.Println("Elements after sorting in lexicographical order.") // printing the elements after sorting them in lexicographical order for i := 0; i < 5; i++ { fmt.Print(stringSlice[i], " ") } fmt.Println() }
输出
Program to sort the element in lexicographical order within the function. Elements before sorting in lexicographical order. Tutorial Point Java C++ Golang Elements after sorting in lexicographical order. C++ Golang Java Point Tutorial
算法
步骤1 − 创建并初始化一个字符串类型的切片。
步骤2 − 调用函数并将切片作为参数传递。
步骤3 − 在被调用的函数中,我们运行嵌套的for循环,在循环内,我们从左侧选择每个索引,并与大于该索引的索引进行比较,并将剩余元素中的最小元素存储起来。
步骤4 − 打印排序后的元素。
示例
在这个例子中,我们将在一个单独的函数中按字典序对元素进行排序。
package main // fmt package provides the function to print anything import "fmt" // this function has a parameter of type string slice func sortElementLexicographical(stringSlice []string) { var temp string // running the nested loops and picking one index at a time // and find the right element in lexicographical order // on that index for i := 0; i < 5; i++ { for j := i + 1; j < 5; j++ { // comparing the strings at index i and index j // if string at index i is greater in lexicographical // order than doing the swap of both elements if stringSlice[i] > stringSlice[j] { temp = stringSlice[i] stringSlice[i] = stringSlice[j] stringSlice[j] = temp } } } } func main() { // creating a slice of string type and storing the element stringSlice := [5]string{"Tutorial", "Point", "Java", "C++", "Golang"} fmt.Println("Program to sort the element in lexicographical order in the separate function.") fmt.Println() fmt.Println("Elements before sorting in lexicographical order.") // printing the elements before sorting them in lexicographical order for i := 0; i < 5; i++ { fmt.Print(stringSlice[i], " ") } fmt.Println() // calling the function by passing the stringSlice as an argument sortElementLexicographical(stringSlice[:]) fmt.Println() fmt.Println("Elements after sorting in lexicographical order.") // printing the elements after sorting them in lexicographical order for i := 0; i < 5; i++ { fmt.Print(stringSlice[i], " ") } fmt.Println() }
输出
Program to sort the element in lexicographical order in the separate function. Elements before sorting in lexicographical order. Tutorial Point Java C++ Golang Elements after sorting in lexicographical order. C++ Golang Java Point Tutorial
算法
步骤1 − 创建并初始化一个字符串类型的切片。
步骤2 − 调用sort包中的函数并将切片作为参数传递。
步骤3 − 打印排序后的元素。
示例
在这个例子中,我们将使用sort包及其函数来实现这一点。
package main // fmt package provides the function to print anything import ( "fmt" "sort" ) func main() { // creating a slice of string type and storing the element stringSlice := [5]string{"Tutorial", "Point", "Java", "C++", "Golang"} fmt.Println("Program to sort the element in lexicographical order using sort package.") fmt.Println() fmt.Println("Elements before sorting in lexicographical order.") // printing the elements before sorting them in lexicographical order for i := 0; i < 5; i++ { fmt.Print(stringSlice[i], " ") } fmt.Println() // calling the function in the sort package by passing the stringSlice as an argument sort.Strings(stringSlice[:]) fmt.Println() fmt.Println("Elements after sorting in lexicographical order.") // printing the elements after sorting them in lexicographical order for i := 0; i < 5; i++ { fmt.Print(stringSlice[i], " ") } fmt.Println() }
输出
Program to sort the element in lexicographical order using sort package. Elements before sorting in lexicographical order. Tutorial Point Java C++ Golang Elements after sorting in lexicographical order. C++ Golang Java Point Tutorial
结论
这是在Go语言中按字典序对元素进行排序的两种方法。第二种方法在模块化和代码可重用性方面更好,因为我们可以在项目的任何地方调用该函数。要了解更多关于Go语言的信息,您可以浏览这些教程。
广告