Golang sort.SliceStable() Function with Examples

Golang | sort.SliceStable() Function: Here, we are going to learn about the SliceStable() function of the sort package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 14, 2021

sort.SliceStable()

The SliceStable() function is an inbuilt function of the sort package which is used to sort the slice x given the provided less function, keeping equal elements in their original order. It may create panic if x is not a slice. Where x is an interface and less is a function.

It accepts two parameters (x interface{}, less func(i, j int) bool) – x is the slice of type interface and less is bool function.

Note: The SliceStable() result may create panic if the given interface (x) is not a slice.

Syntax:

func SliceStable(x interface{}, less func(i, j int) bool)

Parameter(s):

  • x : A slice (interface type) to be sorted.
  • less : A bool function that must satisfy the same requirements as the Interface type's less method.

Return Value:

The SliceStable() function does not return any value.

Example 1:

// Golang program to demonstrate the
// example of sort.SliceStable() Function

package main

import (
	"fmt"
	"sort"
)

func main() {
	student := []struct {
		Name string
		Age  int
	}{
		{"Bobby", 21},
		{"Carry", 23},
		{"Alex", 20},
		{"Denny", 18},
		{"Carry", 20},
		{"Bobby", 21},
		{"Alex", 28},
	}

	// Printing student
	fmt.Println("Before sorting...")
	fmt.Println(student)
	fmt.Println()

	// Sorting the slice (student) based on Name
	// preserving original order
	sort.SliceStable(student,
		func(i, j int) bool { return student[i].Name < student[j].Name })
	fmt.Println("After sorting (by Name)...")
	fmt.Println(student)
	fmt.Println()

	// Sorting the slice (student) based on Age
	// preserving name order
	sort.SliceStable(student,
		func(i, j int) bool { return student[i].Age < student[j].Age })
	fmt.Println("After sorting (by Age)...")
	fmt.Println(student)
}

Output:

Before sorting...
[{Bobby 21} {Carry 23} {Alex 20} {Denny 18} {Carry 20} {Bobby 21} {Alex 28}]

After sorting (by Name)...
[{Alex 20} {Alex 28} {Bobby 21} {Bobby 21} {Carry 23} {Carry 20} {Denny 18}]

After sorting (by Age)...
[{Denny 18} {Alex 20} {Carry 20} {Bobby 21} {Bobby 21} {Carry 23} {Alex 28}]

Example 2:

// Golang program to demonstrate the
// example of sort.Sort() Function

package main

import (
	"fmt"
	"sort"
	"unicode/utf8"
)

type student struct {
	Name string
	Age  int
}

type SortedByName []student

func (s SortedByName) Len() int {
	return len(s)
}

func (s SortedByName) Less(i, j int) bool {
	iRune, _ := utf8.DecodeRuneInString(s[i].Name)
	jRune, _ := utf8.DecodeRuneInString(s[j].Name)
	return int32(iRune) < int32(jRune)
}

func (s SortedByName) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}

func main() {
	std := []student{
		{"Yashna", 21},
		{"Tanisha", 28},
		{"Carry", 22},
		{"Dev", 34},
	}

	fmt.Println("Before sorting...")
	fmt.Println(std)

	// Sorting by Name
	sort.Sort(SortedByName(std))

	fmt.Println("After sorting...")
	fmt.Println(std)
}

Output:

Before sorting...
[{Yashna 21} {Tanisha 28} {Carry 22} {Dev 34}]
After sorting...
[{Carry 22} {Dev 34} {Tanisha 28} {Yashna 21}]

Golang sort Package »





Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.