В общем увидел такой код и решил попробовать подрефакторить его. Можете оценить мой код?
////////начальная версия
func MultiSort(tracks []*Track, columns []string) sort.Interface {
return customSort{
tracks,
func(x, y *Track) bool {
fmt.Println(columns)
for i := len(columns) - 1; i >= 0; i-- {<code lang="cpp">
if columns[i] == "Title" && x.Title != y.Title {
return x.Title < y.Title
} else if columns[i] == "Artist" && x.Artist != y.Artist {
return x.Artist < y.Artist
} else if columns[i] == "Album" && x.Album != y.Album {
return x.Album < y.Album
} else if columns[i] == "Year" && x.Year != y.Year {
return x.Year < y.Year
} else if columns[i] == "Length" && x.Length != y.Length {
return x.Length < y.Length
}
}
return true
},
}
}
////////моя версия
type CompRules []CompRule
type CompRule struct{
name string
cond bool
result bool
}
func (d *CompRules)getRulesResult(name string)(bool,bool){
for _, rule := range *d {
if rule.name == name && rule.cond {
return true, rule.result
}
}
return false, false
}
func initRules(x,y *Track)*CompRules{
return &CompRules{
{"Title", x.Title != y.Title, x.Title < y.Title},
{"Artist", x.Artist != y.Artist, x.Artist < y.Artist},
{"Album", x.Album != y.Album, x.Album < y.Album},
{"Year", x.Year != y.Year, x.Year < y.Year},
{"Length", x.Length != y.Length, x.Length < y.Length},
}
}
func MultiSort(tracks []*Track, columns []string) sort.Interface {
return customSort{
tracks,
func(x, y *Track) bool {
var r = initRules(x,y)
for i := len(columns) - 1; i >= 0; i-- {
if ok, res := r.getRulesResult(columns[i]);ok{
return res
}
}
return true
},
}
}
</code>