Golang program to sort Employee by Name


There may be scenarios in which you need a list of employees and you need to display them in a particular order based on their name initials. In this Golang article, we are going to sort employee names using bubble sort, insertion sort as well as using go sort package.

Algorithm

  • Create a field called “Name” in the Employee struct.

  • Use the array of Employee objects as input for the “BubbleSortByEmployeeName” method.

  • Obtain the employees array's length and save it in the variable n.

  • Start the outer loop from i = 0 to n-1 and Start the inner loop from j = 0 to n-i-1.

  • Compare employees[j].Name with employees[j+1].Name.

  • The function “BubbleSortByEmployeeName” returns.

  • Create an array of Employee objects with the name employees in the main method.

  • Print the names of all the workers by iterating over the employees array.

  • Use the employees array as a parameter when using the BubbleSortByEmployeeName method.

  • Print the names of all the workers by iterating over the employees array.

Example 1: Bubble Sort

Bubble sort is a simple algorithm that compares the adjacent alphabets and swaps them if they are in the wrong order.

package main
import "fmt"

type Employee struct {
   Name string
}

func BubbleSortByEmployeeName(employees []Employee) {
   n := len(employees)
   for i := 0; i < n-1; i++ {
      for j := 0; j < n-i-1; j++ {
         if employees[j].Name > employees[j+1].Name {
            employees[j], employees[j+1] = employees[j+1], employees[j]
         }
      }
   }
}

func main() {
   employees := []Employee{
      {Name: "Nitin"},
      {Name: "Akshay"},
      {Name: "Sahil"},
      {Name: "Akhil"},
   }

   fmt.Println("Before sorting:")
   for _, employee := range employees {
      fmt.Println(employee.Name)
   }

   BubbleSortByEmployeeName(employees)

   fmt.Println("\nAfter sorting:")
   for _, employee := range employees {
      fmt.Println(employee.Name)
   }
}

Output

Before sorting:
Nitin 
Akshay 
Sahil
Akhil 

After sorting: 
Akhil 
Akshay 
Nitin 
Sahil

Example 2: Insertion Sort

Insertion sort is also a simple sorting algorithm that builds the final sorted array by using one item at a time. It works by inserting an element from the unsorted portion into the correct position in the sorted array.

package main
import "fmt"

type Employee struct {
   Name string
}

func InsertionSortByEmployeeName(employees []Employee) {
   n := len(employees)
   for i := 1; i < n; i++ {
      key := employees[i]
      j := i - 1
      for j >= 0 && employees[j].Name > key.Name {
         employees[j+1] = employees[j]
         j--
      }
      employees[j+1] = key
   }
}

func main() {
   employees := []Employee{
      {Name: "Krishan"},
      {Name: "Ram"},
      {Name: "Seeta"},
      {Name: "Radha"},
   }

   fmt.Println("Before sorting:")
   for _, employee := range employees {
      fmt.Println(employee.Name)
   }

   InsertionSortByEmployeeName(employees)

   fmt.Println("\nAfter sorting:")
   for _, employee := range employees {
      fmt.Println(employee.Name)
   }
}

Code

Before sorting: 
Krishan 
Ram 
Seeta 
Radha 

After sorting: 
Krishan 
Radha 
Ram 
Seeta

Example 3: Using Go Sort Package

Sort.sort() is a built-in Golang function that supports multiple sorting algorithms and interfaces. It provides a more efficient and adaptable method for sorting data structures.

package main

import (
   "fmt"
   "sort"
)

type Employee struct {
   Name string
}

type EmployeeSortByName []Employee

func (e EmployeeSortByName) Len() int {
   return len(e)
}

func (e EmployeeSortByName) Less(i, j int) bool {
   return e[i].Name < e[j].Name
}

func (e EmployeeSortByName) Swap(i, j int) {
   e[i], e[j] = e[j], e[i]
}

func main() {
   employees := []Employee{
      {Name: "Krishan"},
      {Name: "Ram"},
      {Name: "Radha"},
      {Name: "Seeta"},
   }

   fmt.Println("Before sorting:")
   for _, employee := range employees {
      fmt.Println(employee.Name)
   }

   sort.Sort(EmployeeSortByName(employees))

   fmt.Println("\nAfter sorting:")
   for _, employee := range employees {
      fmt.Println(employee.Name)
   }
}

Output

Before sorting: 
Krishan 
Ram 
Radha 
Seeta 

After sorting: 
Krishan 
Radha 
Ram 
Seeta

Conclusion

In this article we looked at three distinct approaches to sort a set of Employee objects in Go by their names. Although the implementations for the Bubble Sort and Insertion Sort algorithms are straightforward, they might not be the most effective for huge data sets. In contrast, Go's sort package provides a more reliable and effective solution by utilizing a variety of sorting algorithms.

Updated on: 06-Jul-2023

72 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements