Home  >  Article  >  Backend Development  >  How to sort string array by length using sort function in Go language?

How to sort string array by length using sort function in Go language?

PHPz
PHPzOriginal
2023-07-31 15:01:581712browse

How to use the sort function in Go language to sort a string array by length?

Sort is one of the common operations in programs. String arrays can be sorted in alphabetical order or by string length. This article will introduce how to use the sort function in the Go language to sort a string array by length, and provide code examples for demonstration.

In Go language, you can use the Sort function in the sort package to sort slices. The Sort function requires a slice and a sorting function as parameters. First, we need to define a custom sorting function, which will be used to determine the size relationship between two strings.

The code example is as follows:

package main

import (
    "fmt"
    "sort"
)

type ByLength []string

func (s ByLength) Len() int {
    return len(s)
}

func (s ByLength) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}

func (s ByLength) Less(i, j int) bool {
    return len(s[i]) < len(s[j])
}

func main() {
    strings := []string{"apple", "banana", "cherry", "date"}
    fmt.Println("Before sorting:", strings)

    sort.Sort(ByLength(strings))
    fmt.Println("After sorting by length:", strings)
}

In this example, we create a custom type ByLength, which is a string slice. We have implemented three methods in the sort.Interface interface for the ByLength type: Len(), Swap() and Less(). The Len() method returns the length of the slice, the Swap() method is used to swap the positions of two elements, and the Less() method is used to determine the size of the two elements.

In the main function, we create a string slice strings and call the Sort function to sort it. Since we have implemented the sorting method of the ByLength type, we can directly convert strings to the ByLength type for sorting. Finally, we print out the results before and after sorting.

Execute the above code, and the output result is as follows:

Before sorting: [apple banana cherry date]
After sorting by length: [date apple banana cherry]

As you can see, the string array is sorted from short to long according to length.

Through the above code example, we can see how to use the sort function in the Go language to sort the string array by length. Using the sort.Sort function and custom sorting methods, various sorting needs can be flexibly handled. I hope this article will help you understand and use the sorting function in Go language.

The above is the detailed content of How to sort string array by length using sort function in Go language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn