summaryrefslogtreecommitdiff
path: root/doc/progs/sortmain.go
blob: 74d1d184087e013813d3d777d544b2b3dc909443 (plain)
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
// 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 "sort"

func ints() {
	data := []int{74, 59, 238, -784, 9845, 959, 905, 0, 0, 42, 7586, -5467984, 7586};
	a := sort.IntArray(data);
	sort.Sort(a);
	if !sort.IsSorted(a) {
		panic()
	}
}

func strings() {
	data := []string{"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
	a := sort.StringArray(data);
	sort.Sort(a);
	if !sort.IsSorted(a) {
		panic()
	}
}

type day struct {
	num        int;
	short_name string;
	long_name  string;
}

type dayArray struct {
	data []*day;
}

func (p *dayArray) Len() int            { return len(p.data); }
func (p *dayArray) Less(i, j int) bool  { return p.data[i].num < p.data[j].num; }
func (p *dayArray) Swap(i, j int)       { p.data[i], p.data[j] = p.data[j], p.data[i]; }

func days() {
	Sunday :=    day{ 0, "SUN", "Sunday" };
	Monday :=    day{ 1, "MON", "Monday" };
	Tuesday :=   day{ 2, "TUE", "Tuesday" };
	Wednesday := day{ 3, "WED", "Wednesday" };
	Thursday :=  day{ 4, "THU", "Thursday" };
	Friday :=    day{ 5, "FRI", "Friday" };
	Saturday :=  day{ 6, "SAT", "Saturday" };
	data := []*day{&Tuesday, &Thursday, &Sunday, &Monday, &Friday};
	a := dayArray{data};
	sort.Sort(&a);
	if !sort.IsSorted(&a) {
		panic()
	}
	for i, d := range data {
		print(d.long_name, " ")
	}
	print("\n")
}


func main() {
	ints();
	strings();
	days();
}