Skip to content

Commit fdb449d

Browse files
committed
Sort tests by name when creating servo score report
Signed-off-by: Nico Burns <[email protected]>
1 parent db2b77e commit fdb449d

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

index.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,44 @@ async function process_chunks (path) {
5959
delete scored_chunk.run_info
6060
result = merge_nonoverlap(result, scored_chunk)
6161
}
62+
63+
sort_test_results(result)
64+
6265
return result
6366
}
6467

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+
65100
async function add_run (runs_dir, chunks_dir, date) {
66101
const new_run = await process_chunks(chunks_dir)
67102
await write_compressed(`./${runs_dir}/${date}.xz`, new_run)

0 commit comments

Comments
 (0)