-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.htm
529 lines (529 loc) · 34.3 KB
/
sort.htm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="./site.css" rel="stylesheet">
<title>sort</title>
</head>
<body>
<div class="container">
<h2 id="pkg-overview">package sort</h2>
<p><code>import "sort"</code>
<p>sort包提供了排序切片和用户自定义数据集的函数。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-package">
<div class="panel-heading" onclick="document.getElementById('ex-package').style.display = document.getElementById('ex-package').style.display=='none'?'block':'none';">Example</div>
<div id="ex-package" class="panel-collapse collapse">
<div class="panel-body">
<pre>package sort_test
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
func (p Person) String() string {
return fmt.Sprintf("%s: %d", p.Name, p.Age)
}
<span class="com">// ByAge implements sort.Interface for []Person based on</span>
<span class="com">// the Age field.</span>
type ByAge []Person
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func Example() {
people := []Person{
{"Bob", 31},
{"John", 42},
{"Michael", 17},
{"Jenny", 26},
}
fmt.Println(people)
sort.Sort(ByAge(people))
fmt.Println(people)
<span class="com">// Output:</span>
<span class="com">// [Bob: 31 John: 42 Michael: 17 Jenny: 26]</span>
<span class="com">// [Michael: 17 Jenny: 26 Bob: 31 John: 42]</span>
}
</pre>
</div>
</div>
</div>
<div class="panel panel-default" id="example-package--SortKeys">
<div class="panel-heading" onclick="document.getElementById('ex-package--SortKeys').style.display = document.getElementById('ex-package--SortKeys').style.display=='none'?'block':'none';">Example (SortKeys)</div>
<div id="ex-package--SortKeys" class="panel-collapse collapse">
<div class="panel-body">
<pre>package sort_test
import (
"fmt"
"sort"
)
<span class="com">// A couple of type definitions to make the units clear.</span>
type earthMass float64
type au float64
<span class="com">// A Planet defines the properties of a solar system object.</span>
type Planet struct {
name string
mass earthMass
distance au
}
<span class="com">// By is the type of a "less" function that defines the ordering of its Planet arguments.</span>
type By func(p1, p2 *Planet) bool
<span class="com">// Sort is a method on the function type, By, that sorts the argument slice according to the function.</span>
func (by By) Sort(planets []Planet) {
ps := &planetSorter{
planets: planets,
by: by, <span class="com">// The Sort method's receiver is the function (closure) that defines the sort order.</span>
}
sort.Sort(ps)
}
<span class="com">// planetSorter joins a By function and a slice of Planets to be sorted.</span>
type planetSorter struct {
planets []Planet
by func(p1, p2 *Planet) bool <span class="com">// Closure used in the Less method.</span>
}
<span class="com">// Len is part of sort.Interface.</span>
func (s *planetSorter) Len() int {
return len(s.planets)
}
<span class="com">// Swap is part of sort.Interface.</span>
func (s *planetSorter) Swap(i, j int) {
s.planets[i], s.planets[j] = s.planets[j], s.planets[i]
}
<span class="com">// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.</span>
func (s *planetSorter) Less(i, j int) bool {
return s.by(&s.planets[i], &s.planets[j])
}
var planets = []Planet{
{"Mercury", 0.055, 0.4},
{"Venus", 0.815, 0.7},
{"Earth", 1.0, 1.0},
{"Mars", 0.107, 1.5},
}
<span class="com">// ExampleSortKeys demonstrates a technique for sorting a struct type using programmable sort criteria.</span>
func Example_sortKeys() {
<span class="com">// Closures that order the Planet structure.</span>
name := func(p1, p2 *Planet) bool {
return p1.name < p2.name
}
mass := func(p1, p2 *Planet) bool {
return p1.mass < p2.mass
}
distance := func(p1, p2 *Planet) bool {
return p1.distance < p2.distance
}
decreasingDistance := func(p1, p2 *Planet) bool {
return !distance(p1, p2)
}
<span class="com">// Sort the planets by the various criteria.</span>
By(name).Sort(planets)
fmt.Println("By name:", planets)
By(mass).Sort(planets)
fmt.Println("By mass:", planets)
By(distance).Sort(planets)
fmt.Println("By distance:", planets)
By(decreasingDistance).Sort(planets)
fmt.Println("By decreasing distance:", planets)
<span class="com">// Output: By name: [{Earth 1 1} {Mars 0.107 1.5} {Mercury 0.055 0.4} {Venus 0.815 0.7}]</span>
<span class="com">// By mass: [{Mercury 0.055 0.4} {Mars 0.107 1.5} {Venus 0.815 0.7} {Earth 1 1}]</span>
<span class="com">// By distance: [{Mercury 0.055 0.4} {Venus 0.815 0.7} {Earth 1 1} {Mars 0.107 1.5}]</span>
<span class="com">// By decreasing distance: [{Mars 0.107 1.5} {Earth 1 1} {Venus 0.815 0.7} {Mercury 0.055 0.4}]</span>
}
</pre>
</div>
</div>
</div>
<div class="panel panel-default" id="example-package--SortMultiKeys">
<div class="panel-heading" onclick="document.getElementById('ex-package--SortMultiKeys').style.display = document.getElementById('ex-package--SortMultiKeys').style.display=='none'?'block':'none';">Example (SortMultiKeys)</div>
<div id="ex-package--SortMultiKeys" class="panel-collapse collapse">
<div class="panel-body">
<pre>package sort_test
import (
"fmt"
"sort"
)
<span class="com">// A Change is a record of source code changes, recording user, language, and delta size.</span>
type Change struct {
user string
language string
lines int
}
type lessFunc func(p1, p2 *Change) bool
<span class="com">// multiSorter implements the Sort interface, sorting the changes within.</span>
type multiSorter struct {
changes []Change
less []lessFunc
}
<span class="com">// Sort sorts the argument slice according to the less functions passed to OrderedBy.</span>
func (ms *multiSorter) Sort(changes []Change) {
ms.changes = changes
sort.Sort(ms)
}
<span class="com">// OrderedBy returns a Sorter that sorts using the less functions, in order.</span>
<span class="com">// Call its Sort method to sort the data.</span>
func OrderedBy(less ...lessFunc) *multiSorter {
return &multiSorter{
less: less,
}
}
<span class="com">// Len is part of sort.Interface.</span>
func (ms *multiSorter) Len() int {
return len(ms.changes)
}
<span class="com">// Swap is part of sort.Interface.</span>
func (ms *multiSorter) Swap(i, j int) {
ms.changes[i], ms.changes[j] = ms.changes[j], ms.changes[i]
}
<span class="com">// Less is part of sort.Interface. It is implemented by looping along the</span>
<span class="com">// less functions until it finds a comparison that is either Less or</span>
<span class="com">// !Less. Note that it can call the less functions twice per call. We</span>
<span class="com">// could change the functions to return -1, 0, 1 and reduce the</span>
<span class="com">// number of calls for greater efficiency: an exercise for the reader.</span>
func (ms *multiSorter) Less(i, j int) bool {
p, q := &ms.changes[i], &ms.changes[j]
<span class="com">// Try all but the last comparison.</span>
var k int
for k = 0; k < len(ms.less)-1; k++ {
less := ms.less[k]
switch {
case less(p, q):
<span class="com">// p < q, so we have a decision.</span>
return true
case less(q, p):
<span class="com">// p > q, so we have a decision.</span>
return false
}
<span class="com">// p == q; try the next comparison.</span>
}
<span class="com">// All comparisons to here said "equal", so just return whatever</span>
<span class="com">// the final comparison reports.</span>
return ms.less[k](p, q)
}
var changes = []Change{
{"gri", "Go", 100},
{"ken", "C", 150},
{"glenda", "Go", 200},
{"rsc", "Go", 200},
{"r", "Go", 100},
{"ken", "Go", 200},
{"dmr", "C", 100},
{"r", "C", 150},
{"gri", "Smalltalk", 80},
}
<span class="com">// ExampleMultiKeys demonstrates a technique for sorting a struct type using different</span>
<span class="com">// sets of multiple fields in the comparison. We chain together "Less" functions, each of</span>
<span class="com">// which compares a single field.</span>
func Example_sortMultiKeys() {
<span class="com">// Closures that order the Change structure.</span>
user := func(c1, c2 *Change) bool {
return c1.user < c2.user
}
language := func(c1, c2 *Change) bool {
return c1.language < c2.language
}
increasingLines := func(c1, c2 *Change) bool {
return c1.lines < c2.lines
}
decreasingLines := func(c1, c2 *Change) bool {
return c1.lines > c2.lines <span class="com">// Note: > orders downwards.</span>
}
<span class="com">// Simple use: Sort by user.</span>
OrderedBy(user).Sort(changes)
fmt.Println("By user:", changes)
<span class="com">// More examples.</span>
OrderedBy(user, increasingLines).Sort(changes)
fmt.Println("By user,<lines:", changes)
OrderedBy(user, decreasingLines).Sort(changes)
fmt.Println("By user,>lines:", changes)
OrderedBy(language, increasingLines).Sort(changes)
fmt.Println("By language,<lines:", changes)
OrderedBy(language, increasingLines, user).Sort(changes)
fmt.Println("By language,<lines,user:", changes)
<span class="com">// Output:</span>
<span class="com">// By user: [{dmr C 100} {glenda Go 200} {gri Smalltalk 80} {gri Go 100} {ken Go 200} {ken C 150} {r Go 100} {r C 150} {rsc Go 200}]</span>
<span class="com">// By user,<lines: [{dmr C 100} {glenda Go 200} {gri Smalltalk 80} {gri Go 100} {ken C 150} {ken Go 200} {r Go 100} {r C 150} {rsc Go 200}]</span>
<span class="com">// By user,>lines: [{dmr C 100} {glenda Go 200} {gri Go 100} {gri Smalltalk 80} {ken Go 200} {ken C 150} {r C 150} {r Go 100} {rsc Go 200}]</span>
<span class="com">// By language,<lines: [{dmr C 100} {ken C 150} {r C 150} {gri Go 100} {r Go 100} {ken Go 200} {glenda Go 200} {rsc Go 200} {gri Smalltalk 80}]</span>
<span class="com">// By language,<lines,user: [{dmr C 100} {ken C 150} {r C 150} {gri Go 100} {r Go 100} {glenda Go 200} {ken Go 200} {rsc Go 200} {gri Smalltalk 80}]</span>
}
</pre>
</div>
</div>
</div>
<div class="panel panel-default" id="example-package--SortWrapper">
<div class="panel-heading" onclick="document.getElementById('ex-package--SortWrapper').style.display = document.getElementById('ex-package--SortWrapper').style.display=='none'?'block':'none';">Example (SortWrapper)</div>
<div id="ex-package--SortWrapper" class="panel-collapse collapse">
<div class="panel-body">
<pre>package sort_test
import (
"fmt"
"sort"
)
type Grams int
func (g Grams) String() string { return fmt.Sprintf("%dg", int(g)) }
type Organ struct {
Name string
Weight Grams
}
type Organs []*Organ
func (s Organs) Len() int { return len(s) }
func (s Organs) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
<span class="com">// ByName implements sort.Interface by providing Less and using the Len and</span>
<span class="com">// Swap methods of the embedded Organs value.</span>
type ByName struct{ Organs }
func (s ByName) Less(i, j int) bool { return s.Organs[i].Name < s.Organs[j].Name }
<span class="com">// ByWeight implements sort.Interface by providing Less and using the Len and</span>
<span class="com">// Swap methods of the embedded Organs value.</span>
type ByWeight struct{ Organs }
func (s ByWeight) Less(i, j int) bool { return s.Organs[i].Weight < s.Organs[j].Weight }
func Example_sortWrapper() {
s := []*Organ{
{"brain", 1340},
{"heart", 290},
{"liver", 1494},
{"pancreas", 131},
{"prostate", 62},
{"spleen", 162},
}
sort.Sort(ByWeight{s})
fmt.Println("Organs by weight:")
printOrgans(s)
sort.Sort(ByName{s})
fmt.Println("Organs by name:")
printOrgans(s)
<span class="com">// Output:</span>
<span class="com">// Organs by weight:</span>
<span class="com">// prostate (62g)</span>
<span class="com">// pancreas (131g)</span>
<span class="com">// spleen (162g)</span>
<span class="com">// heart (290g)</span>
<span class="com">// brain (1340g)</span>
<span class="com">// liver (1494g)</span>
<span class="com">// Organs by name:</span>
<span class="com">// brain (1340g)</span>
<span class="com">// heart (290g)</span>
<span class="com">// liver (1494g)</span>
<span class="com">// pancreas (131g)</span>
<span class="com">// prostate (62g)</span>
<span class="com">// spleen (162g)</span>
}
func printOrgans(s []*Organ) {
for _, o := range s {
fmt.Printf("%-8s (%v)\n", o.Name, o.Weight)
}
}
</pre>
</div>
</div>
</div>
</div>
<h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">¶</a></h3>
<a href="../main.html"><h3>返回首页</h3></a>
</br>
<li><a href="#Interface">type Interface</a></li>
<li><a href="#IntSlice">type IntSlice</a></li>
<ul>
<li><a href="#IntSlice.Len">func (p IntSlice) Len() int</a></li>
<li><a href="#IntSlice.Less">func (p IntSlice) Less(i, j int) bool</a></li>
<li><a href="#IntSlice.Search">func (p IntSlice) Search(x int) int</a></li>
<li><a href="#IntSlice.Sort">func (p IntSlice) Sort()</a></li>
<li><a href="#IntSlice.Swap">func (p IntSlice) Swap(i, j int)</a></li>
</ul>
<li><a href="#Float64Slice">type Float64Slice</a></li>
<ul>
<li><a href="#Float64Slice.Len">func (p Float64Slice) Len() int</a></li>
<li><a href="#Float64Slice.Less">func (p Float64Slice) Less(i, j int) bool</a></li>
<li><a href="#Float64Slice.Search">func (p Float64Slice) Search(x float64) int</a></li>
<li><a href="#Float64Slice.Sort">func (p Float64Slice) Sort()</a></li>
<li><a href="#Float64Slice.Swap">func (p Float64Slice) Swap(i, j int)</a></li>
</ul>
<li><a href="#StringSlice">type StringSlice</a></li>
<ul>
<li><a href="#StringSlice.Len">func (p StringSlice) Len() int</a></li>
<li><a href="#StringSlice.Less">func (p StringSlice) Less(i, j int) bool</a></li>
<li><a href="#StringSlice.Search">func (p StringSlice) Search(x string) int</a></li>
<li><a href="#StringSlice.Sort">func (p StringSlice) Sort()</a></li>
<li><a href="#StringSlice.Swap">func (p StringSlice) Swap(i, j int)</a></li>
</ul>
<li><a href="#Ints">func Ints(a []int)</a></li>
<li><a href="#IntsAreSorted">func IntsAreSorted(a []int) bool</a></li>
<li><a href="#SearchInts">func SearchInts(a []int, x int) int</a></li>
<li><a href="#Float64s">func Float64s(a []float64)</a></li>
<li><a href="#Float64sAreSorted">func Float64sAreSorted(a []float64) bool</a></li>
<li><a href="#SearchFloat64s">func SearchFloat64s(a []float64, x float64) int</a></li>
<li><a href="#Strings">func Strings(a []string)</a></li>
<li><a href="#StringsAreSorted">func StringsAreSorted(a []string) bool</a></li>
<li><a href="#SearchStrings">func SearchStrings(a []string, x string) int</a></li>
<li><a href="#Sort">func Sort(data Interface)</a></li>
<li><a href="#Stable">func Stable(data Interface)</a></li>
<li><a href="#Reverse">func Reverse(data Interface) Interface</a></li>
<li><a href="#IsSorted">func IsSorted(data Interface) bool</a></li>
<li><a href="#Search">func Search(n int, f func(int) bool) int</a></li>
</ul>
<h4 id="pkg-examples">Examples <a class="permalink" href="#pkg-index">¶</a></h4>
<a href="../main.html"><h3>返回首页</h3></a>
</br>
<li><a href="#example-Ints" onclick="$('#ex-Ints').addClass('in').removeClass('collapse').height('auto')">Ints</a></li>
<li><a href="#example-Reverse" onclick="$('#ex-Reverse').addClass('in').removeClass('collapse').height('auto')">Reverse</a></li>
<li><a href="#example-package" onclick="$('#ex-package').addClass('in').removeClass('collapse').height('auto')">package</a></li>
<li><a href="#example-package--SortKeys" onclick="$('#ex-package--SortKeys').addClass('in').removeClass('collapse').height('auto')">package (SortKeys)</a></li>
<li><a href="#example-package--SortMultiKeys" onclick="$('#ex-package--SortMultiKeys').addClass('in').removeClass('collapse').height('auto')">package (SortMultiKeys)</a></li>
<li><a href="#example-package--SortWrapper" onclick="$('#ex-package--SortWrapper').addClass('in').removeClass('collapse').height('auto')">package (SortWrapper)</a></li>
</ul>
<h3 id="Interface">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#12">Interface</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>type Interface interface {
<span class="com">// Len方法返回集合中的元素个数</span>
<span id="Interface.Len">Len</span>() <a href="builtin.htm#int">int</a>
<span class="com">// Less方法报告索引i的元素是否比索引j的元素小</span>
<span id="Interface.Less">Less</span>(i, j <a href="builtin.htm#int">int</a>) <a href="builtin.htm#bool">bool</a>
<span class="com">// Swap方法交换索引i和j的两个元素</span>
<span id="Interface.Swap">Swap</span>(i, j <a href="builtin.htm#int">int</a>)
}</pre>
<p>一个满足sort.Interface接口的(集合)类型可以被本包的函数进行排序。方法要求集合中的元素可以被整数索引。</p>
<h3 id="IntSlice">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#233">IntSlice</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>type IntSlice []<a href="builtin.htm#int">int</a></pre>
<p>IntSlice给[]int添加方法以满足Interface接口,以便排序为递增序列。</p>
<h4 id="IntSlice.Len">func (IntSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#235">Len</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#IntSlice">IntSlice</a>) Len() <a href="builtin.htm#int">int</a></pre>
<h4 id="IntSlice.Less">func (IntSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#236">Less</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#IntSlice">IntSlice</a>) Less(i, j <a href="builtin.htm#int">int</a>) <a href="builtin.htm#bool">bool</a></pre>
<h4 id="IntSlice.Swap">func (IntSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#237">Swap</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#IntSlice">IntSlice</a>) Swap(i, j <a href="builtin.htm#int">int</a>)</pre>
<h4 id="IntSlice.Sort">func (IntSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#240">Sort</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#IntSlice">IntSlice</a>) Sort()</pre>
<p>Sort等价于调用Sort(p)</p>
<h4 id="IntSlice.Search">func (IntSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/search.go?name=release#106">Search</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#IntSlice">IntSlice</a>) Search(x <a href="builtin.htm#int">int</a>) <a href="builtin.htm#int">int</a></pre>
<p>Search等价于调用SearchInts(p, x)</p>
<h3 id="Float64Slice">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#243">Float64Slice</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>type Float64Slice []<a href="builtin.htm#float64">float64</a></pre>
<p>Float64Slice给[]float64添加方法以满足Interface接口,以便排序为递增序列。</p>
<h4 id="Float64Slice.Len">func (Float64Slice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#245">Len</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#Float64Slice">Float64Slice</a>) Len() <a href="builtin.htm#int">int</a></pre>
<h4 id="Float64Slice.Less">func (Float64Slice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#246">Less</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#Float64Slice">Float64Slice</a>) Less(i, j <a href="builtin.htm#int">int</a>) <a href="builtin.htm#bool">bool</a></pre>
<h4 id="Float64Slice.Swap">func (Float64Slice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#247">Swap</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#Float64Slice">Float64Slice</a>) Swap(i, j <a href="builtin.htm#int">int</a>)</pre>
<h4 id="Float64Slice.Sort">func (Float64Slice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#255">Sort</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#Float64Slice">Float64Slice</a>) Sort()</pre>
<p>Sort等价于调用Sort(p)</p>
<h4 id="Float64Slice.Search">func (Float64Slice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/search.go?name=release#109">Search</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#Float64Slice">Float64Slice</a>) Search(x <a href="builtin.htm#float64">float64</a>) <a href="builtin.htm#int">int</a></pre>
<p>Search等价于调用SearchFloat64s(p, x)</p>
<h3 id="StringSlice">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#258">StringSlice</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre>type StringSlice []<a href="builtin.htm#string">string</a></pre>
<p>StringSlice给[]string添加方法以满足Interface接口,以便排序为递增序列。</p>
<h4 id="StringSlice.Len">func (StringSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#260">Len</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#StringSlice">StringSlice</a>) Len() <a href="builtin.htm#int">int</a></pre>
<h4 id="StringSlice.Less">func (StringSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#261">Less</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#StringSlice">StringSlice</a>) Less(i, j <a href="builtin.htm#int">int</a>) <a href="builtin.htm#bool">bool</a></pre>
<h4 id="StringSlice.Swap">func (StringSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#262">Swap</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#StringSlice">StringSlice</a>) Swap(i, j <a href="builtin.htm#int">int</a>)</pre>
<h4 id="StringSlice.Sort">func (StringSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#265">Sort</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#StringSlice">StringSlice</a>) Sort()</pre>
<p>Sort等价于调用Sort(p)</p>
<h4 id="StringSlice.Search">func (StringSlice) <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/search.go?name=release#112">Search</a> <a class="permalink" href="#pkg-index">¶</a></h4>
<pre class="funcdecl">func (p <a href="#StringSlice">StringSlice</a>) Search(x <a href="builtin.htm#string">string</a>) <a href="builtin.htm#int">int</a></pre>
<p>Search等价于调用SearchStrings(p, x)</p>
<h3 id="Sort">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#192">Sort</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func Sort(data <a href="#Interface">Interface</a>)</pre>
<p>Sort排序data。它调用1次data.Len确定长度,调用O(n*log(n))次data.Less和data.Swap。本函数不能保证排序的稳定性(即不保证相等元素的相对次序不变)。</p>
<h3 id="Stable">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#317">Stable</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func Stable(data <a href="#Interface">Interface</a>)</pre>
<p align="left">Stable排序data,并保证排序的稳定性,相等元素的相对次序不变。</p>
<p align="left">它调用1次data.Len,O(n*log(n))次data.Less和O(n*log(n)*log(n))次data.Swap。</p>
<h3 id="IsSorted">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#220">IsSorted</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func IsSorted(data <a href="#Interface">Interface</a>) <a href="builtin.htm#bool">bool</a></pre>
<p>IsSorted报告data是否已经被排序。</p>
<h3 id="Reverse">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#215">Reverse</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func Reverse(data <a href="#Interface">Interface</a>) <a href="#Interface">Interface</a></pre>
<p>Reverse包装一个Interface接口并返回一个新的Interface接口,对该接口排序可生成递减序列。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-Reverse">
<div class="panel-heading" onclick="document.getElementById('ex-Reverse').style.display = document.getElementById('ex-Reverse').style.display=='none'?'block':'none';">Example</div>
<div id="ex-Reverse" class="panel-collapse collapse">
<div class="panel-body">
<pre>s := []int{5, 2, 6, 3, 1, 4} <span class="com">// unsorted</span>
sort.Sort(sort.Reverse(sort.IntSlice(s)))
fmt.Println(s)</pre>
<p>Output:
<pre>[6 5 4 3 2 1]
</pre>
</div>
</div>
</div>
</div>
<h3 id="Search">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/search.go?name=release#59">Search</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func Search(n <a href="builtin.htm#int">int</a>, f func(<a href="builtin.htm#int">int</a>) <a href="builtin.htm#bool">bool</a>) <a href="builtin.htm#int">int</a></pre>
<p align="left">Search函数采用二分法搜索找到[0, n)区间内最小的满足f(i)==true的值i。也就是说,Search函数希望f在输入位于区间[0, n)的前面某部分(可以为空)时返回假,而在输入位于剩余至结尾的部分(可以为空)时返回真;Search函数会返回满足f(i)==true的最小值i。如果没有该值,函数会返回n。注意,未找到时的返回值不是-1,这一点和strings.Index等函数不同。Search函数只会用区间[0, n)内的值调用f。</p>
<p align="left">一般使用Search找到值x在插入一个有序的、可索引的数据结构时,应插入的位置。这种情况下,参数f(通常是闭包)会捕捉应搜索的值和被查询的数据集。</p>
<p align="left">例如,给定一个递增顺序的切片,调用Search(len(data), func(i int) bool { return data[i] >= 23 })会返回data中最小的索引i满足data[i] >= 23。如果调用者想要知道23是否在切片里,它必须另外检查data[i] == 23。</p>
<p align="left">搜索递减顺序的数据时,应使用<=运算符代替>=运算符。</p>
<p align="left">下列代码尝试在一个递增顺序的整数切片中找到值x:</p>
<pre>x := 23
i := sort.Search(len(data), func(i int) bool { return data[i] >= x })
if i < len(data) && data[i] == x {
// x is present at data[i]
} else {
// x is not present in data,
// but i is the index where it would be inserted.
}</pre>
<p>一个更古怪的例子,下面的程序会猜测你持有的数字:</p>
<pre>func GuessingGame() {
var s string
fmt.Printf("Pick an integer from 0 to 100.\n")
answer := sort.Search(100, func(i int) bool {
fmt.Printf("Is your number <= %d? ", i)
fmt.Scanf("%s", &s)
return s != "" && s[0] == 'y'
})
fmt.Printf("Your number is %d.\n", answer)
}</pre>
<h3 id="Ints">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#270">Ints</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func Ints(a []<a href="builtin.htm#int">int</a>)</pre>
<p>Ints函数将a排序为递增顺序。</p>
<div class="panel-group">
<div class="panel panel-default" id="example-Ints">
<div class="panel-heading" onclick="document.getElementById('ex-Ints').style.display = document.getElementById('ex-Ints').style.display=='none'?'block':'none';">Example</div>
<div id="ex-Ints" class="panel-collapse collapse">
<div class="panel-body">
<pre>s := []int{5, 2, 6, 3, 1, 4} <span class="com">// unsorted</span>
sort.Ints(s)
fmt.Println(s)</pre>
<p>Output:
<pre>[1 2 3 4 5 6]
</pre>
</div>
</div>
</div>
</div>
<h3 id="IntsAreSorted">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#279">IntsAreSorted</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func IntsAreSorted(a []<a href="builtin.htm#int">int</a>) <a href="builtin.htm#bool">bool</a></pre>
<p>IntsAreSorted检查a是否已排序为递增顺序。</p>
<h3 id="SearchInts">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/search.go?name=release#83">SearchInts</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func SearchInts(a []<a href="builtin.htm#int">int</a>, x <a href="builtin.htm#int">int</a>) <a href="builtin.htm#int">int</a></pre>
<p>SearchInts在递增顺序的a中搜索x,返回x的索引。如果查找不到,返回值是x应该插入a的位置(以保证a的递增顺序),返回值可以是len(a)。</p>
<h3 id="Float64s">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#273">Float64s</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func Float64s(a []<a href="builtin.htm#float64">float64</a>)</pre>
<p>Float64s函数将a排序为递增顺序。</p>
<h3 id="Float64sAreSorted">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#282">Float64sAreSorted</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func Float64sAreSorted(a []<a href="builtin.htm#float64">float64</a>) <a href="builtin.htm#bool">bool</a></pre>
<p>Float64sAreSorted检查a是否已排序为递增顺序。</p>
<h3 id="SearchFloat64s">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/search.go?name=release#92">SearchFloat64s</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func SearchFloat64s(a []<a href="builtin.htm#float64">float64</a>, x <a href="builtin.htm#float64">float64</a>) <a href="builtin.htm#int">int</a></pre>
<p>SearchFloat64s在递增顺序的a中搜索x,返回x的索引。如果查找不到,返回值是x应该插入a的位置(以保证a的递增顺序),返回值可以是len(a)。</p>
<h3 id="Strings">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#276">Strings</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func Strings(a []<a href="builtin.htm#string">string</a>)</pre>
<p>Strings函数将a排序为递增顺序。</p>
<h3 id="StringsAreSorted">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/sort.go?name=release#285">StringsAreSorted</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func StringsAreSorted(a []<a href="builtin.htm#string">string</a>) <a href="builtin.htm#bool">bool</a></pre>
<p>StringsAreSorted检查a是否已排序为递增顺序。</p>
<h3 id="SearchStrings">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/sort/search.go?name=release#101">SearchStrings</a> <a class="permalink" href="#pkg-index">¶</a></h3>
<pre class="funcdecl">func SearchStrings(a []<a href="builtin.htm#string">string</a>, x <a href="builtin.htm#string">string</a>) <a href="builtin.htm#int">int</a></pre>
<p>SearchStrings在递增顺序的a中搜索x,返回x的索引。如果查找不到,返回值是x应该插入a的位置(以保证a的递增顺序),返回值可以是len(a)。</p>
</div>
</body>
</html>