如何在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語言的資訊,您可以瀏覽這些教程。

更新於:2022年11月29日

2K+ 次瀏覽

啟動你的職業生涯

透過完成課程獲得認證

開始學習
廣告