ADVERTISEMENT
ADVERTISEMENT

Golang sort.Slice() Function with Examples

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

sort.Slice()

The Slice() function is an inbuilt function of the sort package which is used to sort the slice x given the provided less function. 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 Slice() result may create panic if the given interface (x) is not a slice.

Syntax:

func Slice(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 Slice() function does not return any value.

Example 1:

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

package main

import (
	"fmt"
	"sort"
)

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

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

	// Sorting the slice (student) based on Name
	sort.Slice(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
	sort.Slice(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}]

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

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

Example 2:

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

package main

import (
	"fmt"
	"sort"
)

func main() {
	items := []struct {
		Name  string
		Qty   int
		Price float64
	}{
		{"iPhone11", 10, 70000},
		{"iWatch", 25, 33999.90},
		{"iPad", 5, 124999.99},
	}

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

	// Sorting the slice (items) based on Item Name
	sort.Slice(items, func(i, j int) bool { return items[i].Name < items[j].Name })
	fmt.Println("After sorting (by Item Name)...")
	fmt.Println(items)
	fmt.Println()

	// Sorting the slice (items) based on Quantity
	sort.Slice(items, func(i, j int) bool { return items[i].Qty < items[j].Qty })
	fmt.Println("After sorting (by Quantity)...")
	fmt.Println(items)
	fmt.Println()

	// Sorting the slice (items) based on Price
	sort.Slice(items, func(i, j int) bool { return items[i].Price < items[j].Price })
	fmt.Println("After sorting (by Price)...")
	fmt.Println(items)
}

Output:

Before sorting...
[{iPhone11 10 70000} {iWatch 25 33999.9} {iPad 5 124999.99}]

After sorting (by Item Name)...
[{iPad 5 124999.99} {iPhone11 10 70000} {iWatch 25 33999.9}]

After sorting (by Quantity)...
[{iPad 5 124999.99} {iPhone11 10 70000} {iWatch 25 33999.9}]

After sorting (by Price)...
[{iWatch 25 33999.9} {iPhone11 10 70000} {iPad 5 124999.99}]

Golang sort Package »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.