×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang sort.SliceIsSorted() Function with Examples

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

sort.SliceIsSorted()

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

Syntax

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

Parameters

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

Return Value

The return type of the SliceIsSorted() function is a bool, it returns true if the given slice is sorted according to the provided less function; false, otherwise.

Example 1

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

package main

import (
	"fmt"
	"sort"
)

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

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

	// Checking whether slice is sorted by Name or not
	result := sort.SliceIsSorted(student,
		func(i, j int) bool { return student[i].Name < student[j].Name })
	fmt.Println("Is sorted by Name?", result)

	// Checking whether slice is sorted by Age or not
	result = sort.SliceIsSorted(student,
		func(i, j int) bool { return student[i].Age < student[j].Age })
	fmt.Println("Is sorted by Age?", result)
	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)

	// Checking again whether slice is sorted by Name or not
	result = sort.SliceIsSorted(student,
		func(i, j int) bool { return student[i].Name < student[j].Name })
	fmt.Println("Is sorted by Name?", result)
	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)

	// Checking whether slice is sorted by Age or not
	result = sort.SliceIsSorted(student,
		func(i, j int) bool { return student[i].Age < student[j].Age })
	fmt.Println("Is sorted by Age?", result)
}

Output:

Before sorting...
[{Bobby 21} {Carry 23} {Alex 20} {Denny 18}]
Is sorted by Name? false
Is sorted by Age? false

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

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

Example 2

// Golang program to demonstrate the
// example of sort.SliceIsSorted() 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)

	// Checking whether slice is sorted by Item Name or not
	result := sort.SliceIsSorted(items,
		func(i, j int) bool { return items[i].Name < items[j].Name })
	fmt.Println("Is sorted by Item Name?", result)

	// Checking whether slice is sorted by Quantity
	result = sort.SliceIsSorted(items,
		func(i, j int) bool { return items[i].Qty < items[j].Qty })
	fmt.Println("Is sorted by Quantity?", result)

	// Checking whether slice is sorted by Price
	result = sort.SliceIsSorted(items,
		func(i, j int) bool { return items[i].Price < items[j].Price })
	fmt.Println("Is sorted by Price?", result)

	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)

	// Checking whether slice is sorted by Item Name or not
	result = sort.SliceIsSorted(items,
		func(i, j int) bool { return items[i].Name < items[j].Name })
	fmt.Println("Is sorted by Item Name?", result)

	// Checking whether slice is sorted by Quantity
	result = sort.SliceIsSorted(items,
		func(i, j int) bool { return items[i].Qty < items[j].Qty })
	fmt.Println("Is sorted by Quantity?", result)

	// Checking whether slice is sorted by Price
	result = sort.SliceIsSorted(items,
		func(i, j int) bool { return items[i].Price < items[j].Price })
	fmt.Println("Is sorted by Price?", result)
}

Output:

Before sorting...
[{iPhone11 10 70000} {iWatch 25 33999.9} {iPad 5 124999.99}]
Is sorted by Item Name? false
Is sorted by Quantity? false
Is sorted by Price? false

After sorting (by Quantity)...
[{iPad 5 124999.99} {iPhone11 10 70000} {iWatch 25 33999.9}]
Is sorted by Item Name? true
Is sorted by Quantity? true
Is sorted by Price? false

Golang sort Package »




Comments and Discussions!

Load comments ↻





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