Skip to content

Commit dd52f08

Browse files
committed
fix: sort rows as a whole
1 parent b28998c commit dd52f08

File tree

1 file changed

+1
-17
lines changed

1 file changed

+1
-17
lines changed

ch7/exercise7.8/main.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,54 +33,38 @@ func click(t string) {
3333
sort.Stable(custom{tracks,
3434
func(x, y *Track) bool {
3535
return x.Title < y.Title
36-
},
37-
func(x, y *Track) {
38-
x.Title, y.Title = y.Title, x.Title
3936
}})
4037
case "artist":
4138
sort.Stable(custom{tracks,
4239
func(x, y *Track) bool {
4340
return x.Artist < y.Artist
44-
},
45-
func(x, y *Track) {
46-
x.Artist, y.Artist = y.Artist, x.Artist
4741
}})
4842
case "album":
4943
sort.Stable(custom{tracks,
5044
func(x, y *Track) bool {
5145
return x.Album < y.Album
52-
},
53-
func(x, y *Track) {
54-
x.Album, y.Album = y.Album, x.Album
5546
}})
5647
case "year":
5748
sort.Stable(custom{tracks,
5849
func(x, y *Track) bool {
5950
return x.Year < y.Year
60-
},
61-
func(x, y *Track) {
62-
x.Year, y.Year = y.Year, x.Year
6351
}})
6452
case "length":
6553
sort.Stable(custom{tracks,
6654
func(x, y *Track) bool {
6755
return int64(x.Length) < int64(y.Length)
68-
},
69-
func(x, y *Track) {
70-
x.Length, y.Length = y.Length, x.Length
7156
}})
7257
}
7358
}
7459

7560
type custom struct {
7661
t []*Track
7762
less func(x, y *Track) bool
78-
swap func(x, y *Track)
7963
}
8064

8165
func (x custom) Len() int { return len(x.t) }
8266
func (x custom) Less(i, j int) bool { return x.less(x.t[i], x.t[j]) }
83-
func (x custom) Swap(i, j int) { x.swap(x.t[i], x.t[j]) }
67+
func (x custom) Swap(i, j int) { x.t[i], x.t[j] = x.t[j], x.t[i] }
8468

8569
type Track struct {
8670
Title string

0 commit comments

Comments
 (0)