improve alpha sort in mflag

Docker-DCO-1.1-Signed-off-by: Victor Vieux <victor.vieux@docker.com> (github: vieux)
This commit is contained in:
Victor Vieux 2014-03-07 02:20:59 +00:00
parent 0718fb409c
commit 6c4652a766

View file

@ -286,9 +286,24 @@ type Flag struct {
DefValue string // default value (as text); for usage message
}
type flagSlice []string
func (p flagSlice) Len() int { return len(p) }
func (p flagSlice) Less(i, j int) bool {
pi, pj := p[i], p[j]
if pi[0] == '-' {
pi = pi[1:]
}
if pj[0] == '-' {
pj = pj[1:]
}
return pi < pj
}
func (p flagSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// sortFlags returns the flags as a slice in lexicographical sorted order.
func sortFlags(flags map[string]*Flag) []*Flag {
var list sort.StringSlice
var list flagSlice
for _, f := range flags {
fName := strings.TrimPrefix(f.Names[0], "#")
if len(f.Names) == 1 {
@ -307,7 +322,7 @@ func sortFlags(flags map[string]*Flag) []*Flag {
list = append(list, fName)
}
}
list.Sort()
sort.Sort(list)
result := make([]*Flag, len(list))
for i, name := range list {
result[i] = flags[name]