Golang program to sort Employee by salary


There can be some scenarios in which you have a list of employees and you need to display them in a particular order based on their salaries by sorting them. In this article we are going create a go program to sort the list of employees by salary using sort.slice() functions, sort.interface interface as well as using a custom sorting function.

Syntax

func len(v Type) int

The len() method returns the length of any parameter. It accepts one input, the data type variable whose length we want to know, and returns the integer value that is the variable's length.

func range(variable)

The range function iterates through any data type. To utilise this, first type the range keyword followed by the data type to which we want to iterate, and the loop will iterate until the final element of the variable is reached.

Method 1: Using sort.slice() Function

This method provides a very easy way to sort slices. By using the sort.slice() function we can easily sort the slices in any order ascending or descending.

Algorithm

  • Create a "employees" slice of Employee objects and populate it with Employee data.

  • Create a "less" comparison function that accepts two indices (i and j) as inputs.

  • Compare the wages of employees[i] with employees[j] under the "less" function.

  • Return true if the pay of employees[i] is less than the salary of employees[j].

  • Return false otherwise.Using the sort, sort the "employees" slice.

  • The slice() method takes as inputs the slice and the "less" function.

  • Using a for-each loop, iterate over the sorted "employees" slice.

  • Using the fmt.Printf() function print each employee's name and salar.

Example

The below example demonstrates a way to use the sort.sort() function to sort a slice based on a particular property.

package main

import (
   "fmt"
   "sort"
)

type Employee struct {
   Name   string
   Salary int
}

func main() {
   employees := []Employee{
      {Name: "Akhil", Salary: 50000},
      {Name: "Akshay", Salary: 30000},
      {Name: "Sahil", Salary: 40000},
   }

   less := func(i, j int) bool {
      return employees[i].Salary < employees[j].Salary
   }

   sort.Slice(employees, less)

   for _, emp := range employees {
      fmt.Printf("Name: %s, Salary: %d\n", emp.Name, emp.Salary)
   }
}

Output

Name: Akshay, Salary: 30000 
Name: Sahil, Salary: 40000 
Name: Akhil, Salary: 50000 

Method 2: Using Sort.interface() Interface

This function allows custom sorting for out data. We can use the go languages built in sort packages to carry out the sorting process by defining the Len(), Less() methods and sort the employee slice.

Algorithm

  • Create a "employees" slice of Employee objects and populate it with Employee data.

  • Create a new type called "BySalary" to represent a subset of Employee objects.

  • Implement the Len(), Swap(), and Less() methods of the sort.Interface interface for the "BySalary" type.

  • Sort the "employees" slice using sort.Sort() and the "BySalary" interface by passing "BySalary(employees)" as a parameter.

  • Using a for-each loop, iterate over the sorted "employees" slice.

  • Using the fmt.Printf() function, print each employee's name and salary.

Example

The example given below is to sort a slice of employee data based on their salaries using the custom type “BySalary”. The program prints the sorted list with the name of the employee and their respective salaries.

package main

import (
   "fmt"
   "sort"
)

type Employee struct {
   Name   string
   Salary int
}

type BySalary []Employee

func (s BySalary) Len() int           { return len(s) }
func (s BySalary) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
func (s BySalary) Less(i, j int) bool { return s[i].Salary < s[j].Salary }

func main() {
   employees := []Employee{
      {Name: "Akhil", Salary: 50000},
      {Name: "Akshay", Salary: 30000},
      {Name: "Sahil", Salary: 40000},
      {Name: "Nitin", Salary: 45000},
      {Name: "Sourab", Salary: 18000},
   }

   sort.Sort(BySalary(employees))

   for _, emp := range employees {
      fmt.Printf("Name: %s, Salary: %d\n", emp.Name, emp.Salary)
   }
}

Output

Name: Sourab, Salary: 18000 
Name: Akshay, Salary: 30000 
Name: Sahil, Salary: 40000 
Name: Nitin, Salary: 45000 
Name: Akhil, Salary: 50000

Method 3: Using Custom Sorting functions with sort.sort()

This method involves a custom sorting function that takes the employee slice and sort it based on the salary field. To perform this operation we can use the sort.sort() function.

Algorithm

  • Begin the main function.

  • Create a "employees" slice of Employee objects and populate it with Employee data.

  • Create a "sortBySalary" function that takes a slice of Employee objects as an input.

  • Sort the "employees" slice using the sort.Sort() function inside the "sortBySalary" and the custom "bySalary" type.

  • Implement the Len(), Swap(), and Less() methods of the sort.Interface interface for the "bySalary" type.

  • Send the "employees" slice as an argument to the "sortBySalary" function.

  • Run a for-each loop through the slice of "employees" that has been sorted.

  • Use the fmt.Printf() function to print the name and pay of each employee.

Example

The code given below sorts a slice named employee based on their salary and then prints the name of the employee and their salaries.

package main

import (
   "fmt"
   "sort"
)

type Employee struct {
   Name   string
   Salary int
}

func sortBySalary(employees []Employee) {
   sort.Sort(bySalary(employees))
}

type bySalary []Employee

func (s bySalary) Len() int { return len(s) }
func (s bySalary) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s bySalary) Less(i, j int) bool { return s[i].Salary < s[j].Salary }

func main() {
   employees := []Employee{
      {Name: "Akhil", Salary: 50000},
      {Name: "Akshay", Salary: 30000},
      {Name: "Sahil", Salary: 40000},
   }

   sortBySalary(employees)

   for _, emp := range employees {
      fmt.Printf("Name: %s, Salary: %d\n", emp.Name, emp.Salary)
   }
}

Output

Name: Akshay, Salary: 30000 
Name: Sahil, Salary: 40000 
Name: Akhil, Salary: 50000 

Conclusion

In this article we have looked at three distinct ways to arrange a group of Employee objects according to their salaries. Data sorting is flexible and simple with Go's Sort() function.

Updated on: 06-Jul-2023

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements