Skip to content

Commit

Permalink
fix: sort rows as a whole
Browse files Browse the repository at this point in the history
  • Loading branch information
linehk committed Jan 15, 2025
1 parent b28998c commit dd52f08
Showing 1 changed file with 1 addition and 17 deletions.
18 changes: 1 addition & 17 deletions ch7/exercise7.8/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,54 +33,38 @@ func click(t string) {
sort.Stable(custom{tracks,
func(x, y *Track) bool {
return x.Title < y.Title
},
func(x, y *Track) {
x.Title, y.Title = y.Title, x.Title
}})
case "artist":
sort.Stable(custom{tracks,
func(x, y *Track) bool {
return x.Artist < y.Artist
},
func(x, y *Track) {
x.Artist, y.Artist = y.Artist, x.Artist
}})
case "album":
sort.Stable(custom{tracks,
func(x, y *Track) bool {
return x.Album < y.Album
},
func(x, y *Track) {
x.Album, y.Album = y.Album, x.Album
}})
case "year":
sort.Stable(custom{tracks,
func(x, y *Track) bool {
return x.Year < y.Year
},
func(x, y *Track) {
x.Year, y.Year = y.Year, x.Year
}})
case "length":
sort.Stable(custom{tracks,
func(x, y *Track) bool {
return int64(x.Length) < int64(y.Length)
},
func(x, y *Track) {
x.Length, y.Length = y.Length, x.Length
}})
}
}

type custom struct {
t []*Track
less func(x, y *Track) bool
swap func(x, y *Track)
}

func (x custom) Len() int { return len(x.t) }
func (x custom) Less(i, j int) bool { return x.less(x.t[i], x.t[j]) }
func (x custom) Swap(i, j int) { x.swap(x.t[i], x.t[j]) }
func (x custom) Swap(i, j int) { x.t[i], x.t[j] = x.t[j], x.t[i] }

type Track struct {
Title string
Expand Down

0 comments on commit dd52f08

Please sign in to comment.