Golang 如何以词表顺序(字典顺序)对元素进行排序

Golang 如何以词表顺序(字典顺序)对元素进行排序

在本教程中,我们将编写Golang程序,对元素按词典顺序进行排序。本教程将包括三种不同的方法来做这件事。为了进行排序,我们需要比较两个字符串,为此,我们将使用<操作符,它将返回一个布尔值。如果左边的值大于右边的按字母顺序排列的值,那么它将返回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()
} 
Go

输出

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 
Go

算法

第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()
} 
Go

输出

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
Go

算法

第1步 - 创建并初始化一个字符串类型的片断。

第2步 - 从sort包中调用一个函数,并将slice作为一个参数传递。

第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()
}
Go

输出

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

总结

以上是在Golang中按词汇顺序排序的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用这个函数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程

登录

注册