Open In App

How to Sort a Slice of Strings in Golang?

Improve
Improve
Like Article
Like
Save
Share
Report

In Go language slice is more powerful, flexible, convenient than an array, and is a lightweight data structure. The slice is a variable-length sequence which stores elements of a similar type, you are not allowed to store different type of elements in the same slice. Go language allows you to sort the elements of the slice according to its type. So, a string type slice is sorted by using the following functions. These functions are defined under the sort package so, you have to import sort package in your program for accessing these functions:

1. Strings: This function is used to only sorts a slice of strings and it sorts the elements of the slice in increasing order.

Syntax:

func Strings(scl []string)

Here, slc represent a slice of strings. Let us discuss this concept with the help of an example:

Example:




// Go program to illustrate how
// to sort the slice of strings
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and initializing slices
    // Using shorthand declaration
    scl1 := []string{"abc", "rwp", "def", "por", "ber", "erj"}
    scl2 := []string{"Rabbit", "Fish", "Dog",
              "Parrot", "Cat", "Hamster"}
  
    // Displaying slices
    fmt.Println("Slices(Before):")
    fmt.Println("Slice 1: ", scl1)
    fmt.Println("Slice 2: ", scl2)
  
    // Sorting the slice of strings
    // Using Strings function
    sort.Strings(scl1)
    sort.Strings(scl2)
  
    // Displaying the result
    fmt.Println("\nSlices(After):")
    fmt.Println("Slice 1 : ", scl1)
    fmt.Println("Slice 2 : ", scl2)
}


Output:

Slices(Before):
Slice 1:  [abc rwp def por ber erj]
Slice 2:  [Rabbit Fish Dog Parrot Cat Hamster]

Slices(After):
Slice 1 :  [abc ber def erj por rwp]
Slice 2 :  [Cat Dog Fish Hamster Parrot Rabbit]

2. StringsAreSorted: This function is used to check whether the given slice of strings is in sorted form(in increasing order) or not. This method returns true if the slice is in sorted form, or return false if the slice is not in the sorted form.

Syntax:

func StringsAreSorted(scl []string) bool

Here, scl represents a slice of strings. Let us discuss this concept with the help of an example:

Example:




// Go program to illustrate how to check
// whether the given slice of strings
// is in sorted form or not
package main
  
import (
    "fmt"
    "sort"
)
  
// Main function
func main() {
  
    // Creating and initializing slices
    // Using shorthand declaration
    scl1 := []string{"abc", "ber", "def", "erj", "por", "rwp"}
    scl2 := []string{"Rabbit", "Fish", "Dog",
                  "Parrot", "Cat", "Hamster"}
  
    // Displaying slices
    fmt.Println("Slices:")
    fmt.Println("Slice 1: ", scl1)
    fmt.Println("Slice 2: ", scl2)
  
    // Checking the slice is in sorted form or not
    // Using StringsAreSorted function
    res1 := sort.StringsAreSorted(scl1)
    res2 := sort.StringsAreSorted(scl2)
  
    // Displaying the result
    fmt.Println("\nResult:")
    fmt.Println("Is Slice 1 is sorted?: ", res1)
    fmt.Println("Is Slice 2 is sorted?: ", res2)
}


Output:

Slices:
Slice 1:  [abc ber def erj por rwp]
Slice 2:  [Rabbit Fish Dog Parrot Cat Hamster]

Result:
Is Slice 1 is sorted?:  true
Is Slice 2 is sorted?:  false


Last Updated : 26 Aug, 2019
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads