Skip to content

Commit

Permalink
Fix instability in key ordering.
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Apr 9, 2019
1 parent fc85683 commit cdc409d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 3 additions & 0 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,9 @@ func (s *S) TestSortedOutput(c *C) {
"d7abc",
"d12",
"d12a",
"e2b",
"e4b",
"e21a",
}
m := make(map[interface{}]int)
for _, k := range order {
Expand Down
8 changes: 7 additions & 1 deletion sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,10 @@ func (l keyList) Less(i, j int) bool {
return ak < bk
}
ar, br := []rune(a.String()), []rune(b.String())
digits := false
for i := 0; i < len(ar) && i < len(br); i++ {
if ar[i] == br[i] {
digits = unicode.IsDigit(ar[i])
continue
}
al := unicode.IsLetter(ar[i])
Expand All @@ -62,7 +64,11 @@ func (l keyList) Less(i, j int) bool {
return ar[i] < br[i]
}
if al || bl {
return bl
if digits {
return al
} else {
return bl
}
}
var ai, bi int
var an, bn int64
Expand Down

0 comments on commit cdc409d

Please sign in to comment.