-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchallenge1.go
57 lines (46 loc) · 1.01 KB
/
challenge1.go
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
package main
import (
"fmt"
"sort"
)
// Flight - a struct that
// contains information about flights
type Flight struct {
Origin string
Destination string
Price int
}
type FlightByPrice []Flight
func (p FlightByPrice) Len() int {
return len(p)
}
func (p FlightByPrice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p FlightByPrice) Less(i, j int) bool {
return p[i].Price > p[j].Price
}
func SortByPrice(flights []Flight) []Flight {
sort.Sort(FlightByPrice(flights))
return flights
}
func printFlights(flights []Flight) {
for _, flight := range flights {
fmt.Printf("Origin: %s, Destination: %s, Price: %d \n", flight.Origin, flight.Destination, flight.Price)
}
}
func main() {
// an empty slice of flights
flights := []Flight{
Flight{Price: 30},
Flight{Price: 20},
Flight{Price: 50},
Flight{Price: 1000},
Flight{Price: 130},
Flight{Price: 20000},
Flight{Price: 550},
Flight{Price: 10},
}
sortedList := SortByPrice(flights)
printFlights(sortedList)
}