blob: c9b18ba93547740e39a017c97c8607ffb122a681 [file] [log] [blame]
Shenghou Ma5b7562d2012-09-03 03:49:03 +08001// cmpout
2
Rob Pike18b21c72011-08-22 22:46:59 +10003// Copyright 2009 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7package main
8
9import (
10 "fmt"
11 "sort"
12)
13
14func main() {
15 seq := Sequence{6, 2, -1, 44, 16}
16 sort.Sort(seq)
17 fmt.Println(seq)
18}
19
20type Sequence []int
21
22// Methods required by sort.Interface.
23func (s Sequence) Len() int {
24 return len(s)
25}
26func (s Sequence) Less(i, j int) bool {
27 return s[i] < s[j]
28}
29func (s Sequence) Swap(i, j int) {
30 s[i], s[j] = s[j], s[i]
31}
32
33// Method for printing - sorts the elements before printing.
34func (s Sequence) String() string {
35 sort.Sort(s)
36 str := "["
37 for i, elem := range s {
38 if i > 0 {
39 str += " "
40 }
41 str += fmt.Sprint(elem)
42 }
43 return str + "]"
44}