Skip to content

Commit

Permalink
Fix unstable map key ordering (#195).
Browse files Browse the repository at this point in the history
  • Loading branch information
niemeyer committed Mar 25, 2018
1 parent 7f97868 commit dcd83b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
9 changes: 8 additions & 1 deletion encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,13 @@ func (s *S) TestSortedOutput(c *C) {
"1",
"2",
"a!10",
"a/2",
"a/0001",
"a/002",
"a/3",
"a/10",
"a/11",
"a/0012",
"a/100",
"a~10",
"ab/1",
"b/1",
Expand All @@ -543,6 +548,8 @@ func (s *S) TestSortedOutput(c *C) {
"c2.10",
"c10.2",
"d1",
"d7",
"d7abc",
"d12",
"d12a",
}
Expand Down
10 changes: 10 additions & 0 deletions sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func (l keyList) Less(i, j int) bool {
}
var ai, bi int
var an, bn int64
if ar[i] == '0' || br[i] == '0' {
j := i
for j > 0 && unicode.IsDigit(ar[j-1]) {
j--
}
if j != i && ar[j] != '0' {
an = 1
bn = 1
}
}
for ai = i; ai < len(ar) && unicode.IsDigit(ar[ai]); ai++ {
an = an*10 + int64(ar[ai]-'0')
}
Expand Down

0 comments on commit dcd83b3

Please sign in to comment.