@@ -59,9 +59,44 @@ async function process_chunks (path) {
59
59
delete scored_chunk . run_info
60
60
result = merge_nonoverlap ( result , scored_chunk )
61
61
}
62
+
63
+ sort_test_results ( result )
64
+
62
65
return result
63
66
}
64
67
68
+ // Sorts the keys of the test_scores object alphabetically
69
+ function sort_test_results ( result ) {
70
+ result . test_scores = sort_object ( result . test_scores )
71
+ }
72
+
73
+ // Sorts the keys of an objects
74
+ //
75
+ // JS objects serialize keys in insertion order, so to control the order of JSON serialized output
76
+ // we can:
77
+ //
78
+ // - Convert the object into an array
79
+ // - Sort the array by test object key
80
+ // - Reinsert the results into a new object in order
81
+ //
82
+ // This sort is not perfect as JS will still serialize keys that can be parsed as integers before
83
+ // all other keys, but it's a lot better than nothing.
84
+ function sort_object ( obj ) {
85
+ const arr = Object . entries ( obj ) . map ( ( [ key , value ] ) => ( { key, value } ) )
86
+ arr . sort ( ( a , b ) => {
87
+ if ( a . key < b . key ) return - 1
88
+ if ( a . key > b . key ) return 1
89
+ return 0
90
+ } )
91
+
92
+ const obj2 = { }
93
+ for ( const entry of arr ) {
94
+ obj2 [ entry . key ] = entry . value
95
+ }
96
+
97
+ return obj2
98
+ }
99
+
65
100
async function add_run ( runs_dir , chunks_dir , date ) {
66
101
const new_run = await process_chunks ( chunks_dir )
67
102
await write_compressed ( `./${ runs_dir } /${ date } .xz` , new_run )
0 commit comments