2014-02-16 56 views
1

我試圖有兩種變體的排序方法:一種按名稱排序元素的形式,另一種按薪水排序元素。 sort.Sort(人(數據))工作時,我較少的方法比較whatever.salary。如果我將其更改爲whatever.name,它也可以工作。我希望能夠在less方法中具體調用這兩個選項,如下面的代碼所示。我的邏輯是使用sort.Sort(people(data.name))作爲名字,sort.Sort(people(data.salary))作爲薪水。這些都不起作用。這甚至可以完成?在Go中,當使用多個返回語句時,如何調用每個特定的語句?

package main 

import (
    "fmt" 
    "sort" 
) 

type Comparable interface { 
    Len() 
    Less(i, j int) bool 
    Swap(i, j int) 
} 

type person struct { 
    name string 
    salary float64 
} 

func (a person) String() string { 
    return fmt.Sprintf("%s: %g \n", a.name, a.salary) 
} 

type people []*person 

func (a people) Len() int { 
    return len(a) 
} 
func (a people) Less(i, j int) bool { 
    return a[i].salary < a[j].salary 
    return a[i].name < a[j].name 
} 
func (a people) Swap(i, j int) { 
    a[i], a[j] = a[j], a[i] 
} 

func main() { 

    var data = make(people, 10) 

    var a, b, c, d, e, f, g, h, i, j person 

    a.name, b.name, c.name, d.name, e.name, f.name, 
     g.name, h.name, i.name, j.name = "Sheila Broflovski", "Ben Affleck", 
     "Mr. Hankey", "Stan Marsh", "Kyle Broflovski", "Eric Cartman", 
     "Kenny McCormick", "Mr. Garrison", "Matt Stone", "Trey Parker" 

    a.salary, b.salary, c.salary, d.salary, e.salary, f.salary, 
     g.salary, h.salary, i.salary, j.salary = 82000, 74000, 0, 400, 
     2500, 1000, 4, 34000, 234000, 234000 
    a.salary = 82000 

    data[0] = &a 
    data[1] = &b 
    data[2] = &c 
    data[3] = &d 
    data[4] = &e 
    data[5] = &f 
    data[6] = &g 
    data[7] = &h 
    data[8] = &i 
    data[9] = &j 

    fmt.Println("\n\n\n") 
    fmt.Print(data) 
    sort.Sort(people(data))  //This works even with the two return statements 
    sort.Sort(people(data.name)) //This does not work. Exist, a version that does? 
    sort.Sort(people(data.salary)) //This does not work. Exist, a version that does? 
    fmt.Println("\n\n\n") 
    fmt.Print(data) 
} 

回答

2

引入排序方法的常用方法是使用描述排序條件的新類型。這裏是byNamebySalary。然後你可以使用sort.Sort(byName(data))進行排序。

這是一些演示代碼。 Go對於構造數據結構也非常有用(例如,在表驅動的測試中多用),這也可以幫助構建您的人員數據。

package main 

import "fmt" 
import "sort" 

type person struct { 
    Name string 
    Salary float64 
} 

type people []*person 

type byName people 
type bySalary people 

func (p byName) Len() int   { return len(p) } 
func (p byName) Less(i, j int) bool { return p[i].Name < p[j].Name } 
func (p byName) Swap(i, j int)  { p[i], p[j] = p[j], p[i] } 

func (p bySalary) Len() int   { return len(p) } 
func (p bySalary) Less(i, j int) bool { return p[i].Salary < p[j].Salary } 
func (p bySalary) Swap(i, j int)  { p[i], p[j] = p[j], p[i] } 

func main() { 
    p := people{ 
     {"Sheila Broflovski", 82000}, 
     {"Ben Affleck", 74000}, 
     {"Mr. Hankey", 0}, 
     {"Stan Marsh", 400}, 
     {"Kyle Broflovski", 2500}, 
     {"Eric Cartman", 1000}, 
     {"Kenny McCormick", 4}, 
     {"Mr. Garrison", 34000}, 
     {"Matt Stone", 234000}, 
     {"Trey Parker", 234000}, 
    } 
    fmt.Println("by name") 
    sort.Sort(byName(p)) 
    for _, x := range p { 
     fmt.Println(*x) 
    } 
    fmt.Println("by salary") 
    sort.Sort(bySalary(p)) 
    for _, x := range p { 
     fmt.Println(*x) 
    } 
} 
1

實現與標準庫sort包第二排序順序,您需要定義實現sort.Interface一個輔助型。爲了處理在你的榜樣工資的情況下,你可以這樣做以下:

type bySalary struct { 
    people 
} 

func (s bySalary) Less(i, j int) bool { 
    return s.people[i].salary < s.people[j].salary 
} 

這裏,sort.Interface所需的LenSwap方法來自於嵌入式people片,而我已經提供了置換比較操作。要排序people數組,你現在可以調用:

sort.Sort(bySalary{data}) 

地使用這種模式,很容易,因爲你需要用很少重複的代碼實現,因爲許多額外的排序鍵。

你可以在這裏玩這個例子:http://play.golang.org/p/kq3SuXMylT