Sorting by Functions

This Go code showcases the use of a custom sorting package named "slices" along with a comparison package "cmp" to perform sorting on slices. It demonstrates the flexibility of custom sorting by providing a comparison function.

Let's go through the code with inline comments and explanations:

// Importing necessary packages.
package main

import (
	"cmp"     // Custom comparison package
	"fmt"
	"slices"  // Custom slices package for sorting
)

// The main function, where the execution of the program begins.
func main() {
	// Example with a slice of strings
	fruits := []string{"peach", "banana", "kiwi"}

	// Defining a custom comparison function for sorting based on string length
	lenCmp := func(a, b string) int {
		return cmp.Compare(len(a), len(b))
	}

	// Using the SortFunc function from the slices package to sort the string slice using the custom comparison function.
	slices.SortFunc(fruits, lenCmp)

	// Printing the sorted string slice.
	fmt.Println(fruits)

	// Example with a slice of custom type (Person)
	type Person struct {
		name string
		age  int
	}

	// Creating a slice of Person instances
	people := []Person{
		{name: "Jax", age: 37},
		{name: "TJ", age: 25},
		{name: "Alex", age: 72},
	}

	// Using the SortFunc function from the slices package to sort the Person slice using a custom comparison function.
	slices.SortFunc(people,
		func(a, b Person) int {
			return cmp.Compare(a.age, b.age)
		})

	// Printing the sorted Person slice.
	fmt.Println(people)
}

Output

[kiwi peach banana]
[{TJ 25} {Jax 37} {Alex 72}]

Now, let's discuss the slices package and the use of SortFunc:

slices.SortFunc(slice interface{}, less func(i, j int) int)

  • This function takes a slice of any type (interface{}) and a custom comparison function less.

  • The comparison function is used to determine the order of elements in the slice.

  • The sorting is performed in-place, modifying the original slice.

The main function demonstrates the use of SortFunc with both a string slice (fruits) and a slice of custom type (people). It shows how to provide a custom comparison function (lenCmp for strings and an anonymous function for Person instances) to achieve sorting based on specific criteria.

Make sure that the slices and cmp packages are implemented correctly and are available in the same directory or in the Go module path for this program to work. The import "slices" and import "cmp" statements assume that there are files named slices.go and cmp.go containing the respective packages in the same directory as your main program.

Last updated