


How to sort string array by length using sort function in Go language?
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

When using sql.Open, why doesn’t the DSN report an error? In Go language, sql.Open...
