← Back to Go Course | Chapter 14: Standard Library & HTTP | Lesson 5 of 10

sort Package का उपयोग

sort package एक बिखरी list को वापस order में लाने का Go का तरीका है, चाहे वह numbers, words, या आपका खुद के लिए order define किया कुछ बिल्कुल custom हो।
Syntax
go
sort.Ints(slice)
sort.Strings(slice)
sort.Slice(slice, func(i, j int) bool {
    return slice[i] < slice[j]
})

Sorting Basic Slices

sort package सबसे आम slice types के लिए सीधे sorting functions प्रदान करता है -- sort.Ints, sort.Strings, और sort.Float64s -- जो दी गई slice को जगह पर, ascending sort करते हैं।

उदाहरण: Sorting Basic Slices

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"sort"
)

// main is where the program starts running
func main() {
	// Short declaration: create nums and infer the type
	nums := []int{5, 2, 8, 1, 9}
	sort.Ints(nums)
	// Print the values followed by a newline
	fmt.Println(nums)
}

Custom Sorting with sort.Slice

sort.Slice किसी भी slice को आपके दिए एक custom comparison function के अनुसार sort करता है, जिसे तब true return करना चाहिए जब index i पर element index j पर element से पहले आता हो।

उदाहरण: Custom Sorting with sort.Slice

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"sort"
)

// Define a struct type named Person
type Person struct {
	Name string
	Age  int
}

// main is where the program starts running
func main() {
	// Short declaration: create people and infer the type
	people := []Person{{"Zara", 25}, {"Amit", 30}, {"Bala", 22}}
	sort.Slice(people, func(i, j int) bool {
		// Send people[i].Age < people[j].Age back to the caller
		return people[i].Age < people[j].Age
	})
	// Print the values followed by a newline
	fmt.Println(people)
}

Checking If a Slice Is Sorted

sort.IntsAreSorted (और इसके counterparts) जांचते हैं कि कोई slice इसे modify किए बिना पहले से ascending order में है या नहीं, binary search या similar पर भरोसा करने से पहले assumptions validate करने के लिए उपयोगी।

उदाहरण: Checking If a Slice Is Sorted

markup
// Every Go file belongs to a package; main builds an executable
package main

// Import the packages this program uses
import (
	"fmt"
	"sort"
)

// main is where the program starts running
func main() {
	// Short declaration: create nums and infer the type
	nums := []int{1, 3, 5, 7}
	// Print the values followed by a newline
	fmt.Println("already sorted:", sort.IntsAreSorted(nums))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. sort.Interface implement करने के बजाय sort.Sort को सीधे एक slice के साथ call करना, या यह भूल जाना कि sort.Slice के comparison function को एक strict less-than boolean return करना चाहिए।
  2. यह मान लेना कि sort.Strings/Ints sort stable है -- stability चाहने वाली custom types के लिए इसके बजाय sort.SliceStable चाहिए।
  3. यह भूल जाना कि sort.Slice slice को जगह पर sort करता है और कुछ return नहीं करता -- reassign करने के लिए कोई return value नहीं।
चैप्टर सारांश
  • sort.Ints, sort.Strings, और sort.Float64s उन basic types की slices को जगह पर sort करते हैं।
  • sort.Slice किसी भी slice को एक custom less-than comparison function उपयोग करके sort करता है।
  • sort.SliceStable साधारण sort.Slice के विपरीत बराबर elements का relative order संरक्षित रखता है।
  • sort.Search पहले से sorted slice पर binary search perform करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.