blob: a77ae7381af1680f2e2eb913cad18eb6848e1296 [file] [log] [blame]
Rob Pikea43033a2008-09-15 11:48:37 -07001// Copyright 2009 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package main
6
Rob Pikebdbb3b42009-03-18 14:09:16 -07007import (
Rob Pike34356e92009-12-16 10:29:53 +11008 "fmt"
Rob Pikee3b61882010-12-09 16:18:31 -05009 "./sort"
Rob Pikebdbb3b42009-03-18 14:09:16 -070010)
Rob Pikea43033a2008-09-15 11:48:37 -070011
12func ints() {
Rob Pike34356e92009-12-16 10:29:53 +110013 data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586}
14 a := sort.IntArray(data)
15 sort.Sort(a)
Rob Pike40d54352009-01-09 15:16:31 -080016 if !sort.IsSorted(a) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070017 panic("fail")
Rob Pikea43033a2008-09-15 11:48:37 -070018 }
19}
20
21func strings() {
Rob Pike34356e92009-12-16 10:29:53 +110022 data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"}
23 a := sort.StringArray(data)
24 sort.Sort(a)
Rob Pike40d54352009-01-09 15:16:31 -080025 if !sort.IsSorted(a) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070026 panic("fail")
Rob Pikea43033a2008-09-15 11:48:37 -070027 }
28}
29
Rob Pike293c8f82009-01-15 17:54:07 -080030type day struct {
Rob Pike34356e92009-12-16 10:29:53 +110031 num int
32 shortName string
33 longName string
Rob Pikea43033a2008-09-15 11:48:37 -070034}
35
Rob Pike293c8f82009-01-15 17:54:07 -080036type dayArray struct {
Rob Pike34356e92009-12-16 10:29:53 +110037 data []*day
Rob Pikea43033a2008-09-15 11:48:37 -070038}
39
Rob Pike34356e92009-12-16 10:29:53 +110040func (p *dayArray) Len() int { return len(p.data) }
41func (p *dayArray) Less(i, j int) bool { return p.data[i].num < p.data[j].num }
42func (p *dayArray) Swap(i, j int) { p.data[i], p.data[j] = p.data[j], p.data[i] }
Rob Pikea43033a2008-09-15 11:48:37 -070043
44func days() {
Russ Cox00f9f0c2010-03-30 10:34:57 -070045 Sunday := day{0, "SUN", "Sunday"}
46 Monday := day{1, "MON", "Monday"}
47 Tuesday := day{2, "TUE", "Tuesday"}
48 Wednesday := day{3, "WED", "Wednesday"}
49 Thursday := day{4, "THU", "Thursday"}
50 Friday := day{5, "FRI", "Friday"}
51 Saturday := day{6, "SAT", "Saturday"}
Rob Pike34356e92009-12-16 10:29:53 +110052 data := []*day{&Tuesday, &Thursday, &Wednesday, &Sunday, &Monday, &Friday, &Saturday}
53 a := dayArray{data}
54 sort.Sort(&a)
Rob Pike40d54352009-01-09 15:16:31 -080055 if !sort.IsSorted(&a) {
Russ Cox00f9f0c2010-03-30 10:34:57 -070056 panic("fail")
Rob Pikea43033a2008-09-15 11:48:37 -070057 }
Russ Coxae54cf72009-09-15 12:42:24 -070058 for _, d := range data {
Rob Pike83f0b712009-11-01 20:47:03 -080059 fmt.Printf("%s ", d.longName)
Rob Pikea43033a2008-09-15 11:48:37 -070060 }
Rob Pike783952132009-10-13 12:37:04 -070061 fmt.Printf("\n")
Rob Pikea43033a2008-09-15 11:48:37 -070062}
63
64
65func main() {
Rob Pike34356e92009-12-16 10:29:53 +110066 ints()
67 strings()
68 days()
Rob Pikea43033a2008-09-15 11:48:37 -070069}