Home | History | Annotate | Download | only in sort

Lines Matching refs:By

2 // Use of this source code is governed by a BSD-style
23 // By is the type of a "less" function that defines the ordering of its Planet arguments.
24 type By func(p1, p2 *Planet) bool
26 // Sort is a method on the function type, By, that sorts the argument slice according to the function.
27 func (by By) Sort(planets []Planet) {
30 by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
35 // planetSorter joins a By function and a slice of Planets to be sorted.
38 by func(p1, p2 *Planet) bool // Closure used in the Less method.
51 // Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
53 return s.by(&s.planets[i], &s.planets[j])
79 // Sort the planets by the various criteria.
80 By(name).Sort(planets)
81 fmt.Println("By name:", planets)
83 By(mass).Sort(planets)
84 fmt.Println("By mass:", planets)
86 By(distance).Sort(planets)
87 fmt.Println("By distance:", planets)
89 By(decreasingDistance).Sort(planets)
90 fmt.Println("By decreasing distance:", planets)
92 // Output: By name: [{Earth 1 1} {Mars 0.107 1.5} {Mercury 0.055 0.4} {Venus 0.815 0.7}]
93 // By mass: [{Mercury 0.055 0.4} {Mars 0.107 1.5} {Venus 0.815 0.7} {Earth 1 1}]
94 // By distance: [{Mercury 0.055 0.4} {Venus 0.815 0.7} {Earth 1 1} {Mars 0.107 1.5}]
95 // By decreasing distance: [{Mars 0.107 1.5} {Earth 1 1} {Venus 0.815 0.7} {Mercury 0.055 0.4}]