How to sort a slice of Search in Golang?


Sorting a slice of string values is a common task in many applications, and Go provides a built-in package sort that includes functions to sort slices of any type, including slices of string values. In this article, we will discuss how to sort a slice of string values that implement the Search interface in Golang.

The sort.Search function in Go is used to perform binary search on a sorted slice. This function takes three arguments: the length of the slice, a function that compares an element in the slice to a given value, and the value to search for. The function returns the index where the value is found or where it should be inserted to maintain the sorted order of the slice.

To sort a slice of string values that implement the Search interface in Go, we can use the sort.Search function along with a custom implementation of the sort.Interface interface.

Example

Here's an example of how to do this −

package main

import (
   "fmt"
   "sort"
)

type Person struct {
   Name string
   Age  int
}

type People []Person

func (p People) Len() int {
   return len(p)
}

func (p People) Swap(i, j int) {
   p[i], p[j] = p[j], p[i]
}

func (p People) Less(i, j int) bool {
   return p[i].Age < p[j].Age
}

func (p People) Search(name string) int {
   return sort.Search(len(p), func(i int) bool {
      return p[i].Name >= name
   })
}

func main() {
   people := People{
      {"Alice", 25},
      {"Bob", 20},
      {"Charlie", 30},
      {"David", 35},
   }

   fmt.Println("Original slice:", people)

   sort.Sort(people)

   fmt.Println("Sorted slice:", people)
   
   fmt.Println("Index of Alice:", people.Search("Alice"))
   fmt.Println("Index of Bob:", people.Search("Bob"))
   fmt.Println("Index of Charlie:", people.Search("Charlie"))
   fmt.Println("Index of David:", people.Search("David"))
}

Output

Original slice: [{Alice 25} {Bob 20} {Charlie 30} {David 35}]
Sorted slice: [{Bob 20} {Alice 25} {Charlie 30} {David 35}]
Index of Alice: 0
Index of Bob: 2
Index of Charlie: 2
Index of David: 3

In the above example, we define a custom type People that represents a slice of Person values. We then implement the sort.Interface interface for this type by defining the Len(), Swap(), and Less() methods. Additionally, we implement the Search() method, which uses the sort.Search function to find the index of a person by their name.

Finally, we create a slice of Person values using this custom type and sort the slice in ascending order by age using the sort.Sort() function. We then use the Search() method to find the index of each person in the sorted slice.

Conclusion

Sorting a slice of string values that implement the Search interface in Golang can be accomplished using the sort.Search function along with a custom implementation of the sort.Interface interface. Understanding how to sort slices of string values is essential for writing efficient and effective Go code.

Updated on: 26-Apr-2023

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements