-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomework8.go
More file actions
93 lines (68 loc) · 1.64 KB
/
homework8.go
File metadata and controls
93 lines (68 loc) · 1.64 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"bufio"
"fmt"
"log"
"os"
"sort"
"strconv"
s "strings"
)
type Person struct {
last_name string
first_name string
movies string
}
func (p Person) String() string {
return fmt.Sprintf("First name: %s, Last name: %s, Series: %s", p.first_name, p.last_name, p.movies)
}
type Sort interface {
sort_the_slice(p int64) list
}
type list []Person
func (people list) sort_the_slice(p int64) {
if p == 0 {
sort.Slice(people, func(i, j int) bool { return people[i].last_name < people[j].last_name })
} else if p == 1 {
sort.Slice(people, func(i, j int) bool { return people[i].first_name < people[j].first_name })
} else {
sort.Slice(people, func(i, j int) bool { return people[i].movies < people[j].movies })
}
}
func main() {
file_name := os.Args[1]
sort_on, err := strconv.ParseInt(os.Args[2], 10, 0)
println(file_name, sort_on)
file_content, err := os.Open(file_name)
defer file_content.Close()
check(err)
scanner := bufio.NewScanner(file_content)
each_person_line := make([]string, 0)
for scanner.Scan() {
each_person_line = append(each_person_line, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
people := []Person{}
for i := range each_person_line {
splice := s.Split(each_person_line[i], ",")
last_name := splice[0]
first_name := splice[1]
movies := splice[2]
persons := Person{last_name: last_name, first_name: first_name, movies: movies}
people = append(people, persons)
i++
}
names := list(people)
names.sort_the_slice(sort_on)
for j := range people {
fmt.Println(people[j])
j++
}
}
func check(e error) {
if e != nil {
log.Fatal(e)
}
}