Golang 如何按字典顺序(字母顺序)对元素进行排序
在本教程中,我们将编写Golang程序以按字典顺序对元素进行排序。本教程将包括三种不同的方法来完成此操作。要进行排序,我们需要比较两个字符串,为此,我们将使用<运算符,它将返回一个布尔值。如果左侧的值在字典顺序上大于右侧的值,则返回false,否则返回true。
例如,
教程 < 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 − 在被调用的函数中,我们在嵌套的循环中选择左边的每个索引,并与大于该索引的索引进行比较,然后将最小的元素存储在剩余元素中。
步骤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 - 调用排序包中的函数,并将切片作为参数传递。
步骤3 - 打印排序后的元素。
示例
在这个示例中,我们将使用排序包及其函数来实现这个功能。
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
结论
这是在Golang中按字典顺序排序元素的两种方式。就模块化和代码可重用性而言,第二种方式要好得多,因为我们可以在项目的任何地方调用那个函数。要了解更多关于Golang的信息,可以探索这些 教程。