File tree Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Expand file tree Collapse file tree 1 file changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -125,6 +125,24 @@ extension CBOR {
125
125
res. append ( contentsOf: arr. flatMap { return $0. encode ( ) } )
126
126
return res
127
127
}
128
+
129
+ // MARK: sortHelper
130
+
131
+ public static func keyOrder( a: [ UInt8 ] , b: [ UInt8 ] ) -> Bool {
132
+ if a. count == b. count {
133
+ for (index, byteA) in a. enumerated ( ) {
134
+ let byteB = b [ index]
135
+ if byteA == byteB {
136
+ continue
137
+ } else {
138
+ return byteA < byteB
139
+ }
140
+ }
141
+ return false
142
+ } else {
143
+ return a. count < b. count
144
+ }
145
+ }
128
146
129
147
// MARK: - major 5: a map of pairs of data items
130
148
@@ -133,10 +151,16 @@ extension CBOR {
133
151
res. reserveCapacity ( 1 + map. count * ( MemoryLayout < A > . size + MemoryLayout < B > . size + 2 ) )
134
152
res = map. count. encode ( )
135
153
res [ 0 ] = res [ 0 ] | 0b101_00000
136
- for (k, v) in map {
137
- res. append ( contentsOf: k. encode ( ) )
138
- res. append ( contentsOf: v. encode ( ) )
154
+
155
+ let sortedEntries = map
156
+ . map { k, v in ( k. encode ( ) , v. encode ( ) ) }
157
+ . sorted { a, b in keyOrder ( a: a. 0 , b: b. 0 ) }
158
+
159
+ for (k, v) in sortedEntries {
160
+ res. append ( contentsOf: k)
161
+ res. append ( contentsOf: v)
139
162
}
163
+
140
164
return res
141
165
}
142
166
You can’t perform that action at this time.
0 commit comments