Skip to content
jsonW Paper GitHub

Search and ranking

The top k, always in the same order

jsonW's top-k uses a heap and is deterministic: highest score first and, on ties, the lower row. Two runs return the same list.

· jsonW 1.3.11

The problem

When two rows have the same score, many systems return whatever order comes out of parallel processing. The same query can change its result from one run to the next, which breaks tests and audits.

How jsonW solves it

The top-k keeps only the k best rows in a heap while it reads, without sorting the whole file. The tie rule is fixed: highest score first and, with equal scores, the row with the lower position.

$ wjsw escolher prod.jsw nota 2
4.9	3	{id:4,produto:"Bolo de milho",…}
4.8	0	{id:1,produto:"Café especial",…}

Real output: the 2 highest values in the column.

Where it is

wjsw escolher file column k for one column, and avaliar for any request. In the SDKs, the escolher(notas, k) function applies the same rule to a list of scores from your application.

Read the full section in the paper →