mirror of
https://github.com/golang/go
synced 2024-11-22 17:24:53 -07:00
18b21c720e
R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/4937045
43 lines
748 B
Go
43 lines
748 B
Go
// Copyright 2009 The Go Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sort"
|
|
)
|
|
|
|
func main() {
|
|
seq := Sequence{6, 2, -1, 44, 16}
|
|
sort.Sort(seq)
|
|
fmt.Println(seq)
|
|
}
|
|
|
|
type Sequence []int
|
|
|
|
// Methods required by sort.Interface.
|
|
func (s Sequence) Len() int {
|
|
return len(s)
|
|
}
|
|
func (s Sequence) Less(i, j int) bool {
|
|
return s[i] < s[j]
|
|
}
|
|
func (s Sequence) Swap(i, j int) {
|
|
s[i], s[j] = s[j], s[i]
|
|
}
|
|
|
|
// Method for printing - sorts the elements before printing.
|
|
func (s Sequence) String() string {
|
|
sort.Sort(s)
|
|
str := "["
|
|
for i, elem := range s {
|
|
if i > 0 {
|
|
str += " "
|
|
}
|
|
str += fmt.Sprint(elem)
|
|
}
|
|
return str + "]"
|
|
}
|