Compare commits
2 Commits
17a8ace39d
...
6a1339b1f2
Author | SHA1 | Date |
---|---|---|
|
6a1339b1f2 | |
|
7a8e1a2a6d |
|
@ -0,0 +1,71 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
|
// 很多图形界面提供了一个有状态的多重排序表格插件:
|
||||||
|
// 主要的排序键是最近一次点击过列头的列,第二个排序键是第二最近点击过列头的列,等等。
|
||||||
|
// 定义一个sort.Interface的实现用在这样的表格中。
|
||||||
|
// 比较这个实现方式和重复使用sort.Stable来排序的方式。
|
||||||
|
|
||||||
|
// 1. 定义数据
|
||||||
|
// 2. 实现sort.Interface
|
||||||
|
// 3. 实现可复用的排序方法
|
||||||
|
// 4. 实现点击一栏就对一栏排序
|
||||||
|
|
||||||
|
// 1. 定义数据类型
|
||||||
|
type Person struct {
|
||||||
|
Name string
|
||||||
|
Age int
|
||||||
|
Sex bool
|
||||||
|
ID int
|
||||||
|
}
|
||||||
|
|
||||||
|
type PersonSlice []Person
|
||||||
|
|
||||||
|
// Less 没有实现sort.Interface的Less方法
|
||||||
|
func (p PersonSlice) Less(i, j int, field string) bool {
|
||||||
|
return PersonByField(p[i], p[j], field)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PersonByField 按照字段排序
|
||||||
|
func PersonByField(i, j Person, field string) bool {
|
||||||
|
switch field {
|
||||||
|
case "Name":
|
||||||
|
return i.Name > j.Name
|
||||||
|
case "Age":
|
||||||
|
return i.Age < j.Age
|
||||||
|
case "Sex":
|
||||||
|
return i.Sex != j.Sex
|
||||||
|
case "ID":
|
||||||
|
return i.ID < j.ID
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
func main() {
|
||||||
|
// 构建数据
|
||||||
|
data := PersonSlice{
|
||||||
|
{"a", 1, true, 1},
|
||||||
|
{"b", 2, false, 2},
|
||||||
|
{"c", 3, true, 3},
|
||||||
|
{"d", 4, false, 4},
|
||||||
|
{"e", 5, true, 5},
|
||||||
|
{"f", 6, false, 6},
|
||||||
|
{"g", 7, true, 7},
|
||||||
|
}
|
||||||
|
// 排序
|
||||||
|
sort.Slice(data, func(i, j int) bool {
|
||||||
|
return data.Less(i, j, "Sex")
|
||||||
|
})
|
||||||
|
// 输出
|
||||||
|
for _, v := range data {
|
||||||
|
fmt.Println(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 尝试使用反射解决,可是发现不会,233333
|
||||||
|
t, b := reflect.TypeOf(data[1]).FieldByName("Name")
|
||||||
|
fmt.Println(t, b)
|
||||||
|
}
|
|
@ -0,0 +1,112 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sort"
|
||||||
|
"text/tabwriter"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Interface interface {
|
||||||
|
// Len 方法返回集合中的元素个数
|
||||||
|
Len() int
|
||||||
|
// Less 方法报告索引i的元素是否比索引j的元素小
|
||||||
|
Less(i, j int) bool
|
||||||
|
// Swap 方法交换索引i和j的两个元素
|
||||||
|
Swap(i, j int)
|
||||||
|
}
|
||||||
|
|
||||||
|
// StringSlice 结构体实现了Interface接口
|
||||||
|
type StringSlice []string
|
||||||
|
|
||||||
|
// Len 方法返回集合中的元素个数
|
||||||
|
func (p StringSlice) Len() int { return len(p) }
|
||||||
|
|
||||||
|
// Less 方法报告索引i的元素是否比索引j的元素小
|
||||||
|
func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] }
|
||||||
|
|
||||||
|
// Swap 交换元素
|
||||||
|
func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
||||||
|
|
||||||
|
type Track struct {
|
||||||
|
Title string
|
||||||
|
Artist string
|
||||||
|
Album string
|
||||||
|
Year int
|
||||||
|
Length time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
var tracks = []*Track{
|
||||||
|
{"Go", "Delilah", "From the Roots Up", 2012, length("3m38s")},
|
||||||
|
{"Go", "Moby", "Moby", 1992, length("3m37s")},
|
||||||
|
{"Go Ahead", "Alicia Keys", "As I Am", 2007, length("4m36s")},
|
||||||
|
{"Ready 2 Go", "Martin Solveig", "Smash", 2011, length("4m24s")},
|
||||||
|
}
|
||||||
|
|
||||||
|
func length(s string) time.Duration {
|
||||||
|
d, err := time.ParseDuration(s)
|
||||||
|
if err != nil {
|
||||||
|
panic(s)
|
||||||
|
}
|
||||||
|
return d
|
||||||
|
}
|
||||||
|
func printTracks(tracks []*Track) {
|
||||||
|
const format = "%v\t%v\t%v\t%v\t%v\t\n"
|
||||||
|
tw := new(tabwriter.Writer).Init(os.Stdout, 0, 8, 2, ' ', 0)
|
||||||
|
fmt.Fprintf(tw, format, "Title", "Artist", "Album", "Year", "Length")
|
||||||
|
fmt.Fprintf(tw, format, "-----", "------", "-----", "----", "------")
|
||||||
|
for _, t := range tracks {
|
||||||
|
fmt.Fprintf(tw, format, t.Title, t.Artist, t.Album, t.Year, t.Length)
|
||||||
|
}
|
||||||
|
tw.Flush() // calculate column widths and print table
|
||||||
|
}
|
||||||
|
|
||||||
|
type byArtist []*Track
|
||||||
|
|
||||||
|
func (x byArtist) Len() int { return len(x) }
|
||||||
|
func (x byArtist) Less(i, j int) bool { return x[i].Artist < x[j].Artist }
|
||||||
|
func (x byArtist) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||||
|
|
||||||
|
type byYear []*Track
|
||||||
|
|
||||||
|
func (x byYear) Len() int { return len(x) }
|
||||||
|
func (x byYear) Less(i, j int) bool { return x[i].Year < x[j].Year }
|
||||||
|
func (x byYear) Swap(i, j int) { x[i], x[j] = x[j], x[i] }
|
||||||
|
|
||||||
|
type customSort struct {
|
||||||
|
t []*Track
|
||||||
|
less func(x, y *Track) bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (x customSort) Len() int { return len(x.t) }
|
||||||
|
func (x customSort) Less(i, j int) bool { return x.less(x.t[i], x.t[j]) }
|
||||||
|
func (x customSort) Swap(i, j int) { x.t[i], x.t[j] = x.t[j], x.t[i] }
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
nums := StringSlice([]string{"b", "f", "c", "a"})
|
||||||
|
sort.Sort(nums)
|
||||||
|
fmt.Println(nums)
|
||||||
|
|
||||||
|
printTracks(tracks)
|
||||||
|
fmt.Println()
|
||||||
|
sort.Sort(byArtist(tracks))
|
||||||
|
printTracks(tracks)
|
||||||
|
fmt.Println()
|
||||||
|
sort.Sort(sort.Reverse(byArtist(tracks)))
|
||||||
|
printTracks(tracks)
|
||||||
|
|
||||||
|
sort.Sort(customSort{tracks, func(x, y *Track) bool {
|
||||||
|
if x.Title != y.Title {
|
||||||
|
return x.Title < y.Title
|
||||||
|
}
|
||||||
|
if x.Year != y.Year {
|
||||||
|
return x.Year < y.Year
|
||||||
|
}
|
||||||
|
if x.Length != y.Length {
|
||||||
|
return x.Length < y.Length
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}})
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue