sort Package का उपयोग
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
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
// 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)
}
Login to try C/C++/Java/PHP code in the editor
Checking If a Slice Is Sorted
sort.IntsAreSorted (और इसके counterparts) जांचते हैं कि कोई slice इसे modify किए बिना पहले से ascending order में है या नहीं, binary search या similar पर भरोसा करने से पहले assumptions validate करने के लिए उपयोगी।
उदाहरण: Checking If a Slice Is Sorted
// 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))
}
Login to try C/C++/Java/PHP code in the editor
- sort.Interface implement करने के बजाय sort.Sort को सीधे एक slice के साथ call करना, या यह भूल जाना कि sort.Slice के comparison function को एक strict less-than boolean return करना चाहिए।
- यह मान लेना कि sort.Strings/Ints sort stable है -- stability चाहने वाली custom types के लिए इसके बजाय sort.SliceStable चाहिए।
- यह भूल जाना कि 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 करता है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: