dongsetan3216 2014-02-16 06:52
浏览 51
已采纳

在Go中,当使用多个return语句时,如何调用每个特定的语句?

I am attempting to have two variations of the sort method: one form which sorts the elements by name, and another that sorts the elements by salary. sort.Sort(people(data)) works when my less method compares whatever.salary. It also works if I change it to whatever.name. I would like to be able to specifically call both of these options in the less method as shown in the below code. My logic is using sort.Sort(people(data.name)) for name, and sort.Sort(people(data.salary)) for salary. These are not working. Can this even be done?

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 
", 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("


")
    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("


")
    fmt.Print(data)
}
  • 写回答

2条回答 默认 最新

  • doujianchao7446 2014-02-16 07:06
    关注

    A common way to introduce the methods for sorting is to use a new type that describes the sorting condition. That's byName and bySalary here. Then you can sort using sort.Sort(byName(data)).

    Here's some demonstration code. Go also has really great good for constructing data-structures (used prolifically, for example, in table-driven tests), which also can help construct your people data here.

    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条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器