zed icon indicating copy to clipboard operation
zed copied to clipboard

Improve top operator

Open nwt opened this issue 2 years ago • 1 comments

The top operator is limited to field references. It should should accept arbitrary expressions like the sort operator does.

It should also allow selection of either ascending or descending order for each expression.

nwt avatar May 12 '23 19:05 nwt

As of Zed commit 4290afd, it looks like top does take arbitrary expressions like sort does, e.g., modifying an example from the sort docs:

$ zq -version
Version: v1.17.0-56-g4290afdc

$ echo '{word:"hello"}{word:"Hi"}{word:"WORLD"}' |   zq -z 'top 2 lower(word)' -
{word:"WORLD"}
{word:"Hi"}

However, it remains true that top lacks the per-key ordering of sort like was added in #5203.

philrz avatar Sep 13 '24 18:09 philrz

Verified in super commit 7a5d7b95.

The same kind of per-key ordering used with sort is now available with top. For instance, we can start with the "Sort by secondary key in reverse order when the primary keys are identical" example from the sort docs and use top 1 instead of sort to get just the first value.

$ super -version
Version: v1.18.0-403-g7a5d7b95c

$ echo '{s:"bar",k:2} {s:"bar",k:3} {s:"foo",k:2}' | super -z -c 'top 1 k,s desc' -
{s:"foo",k:2}

Thanks @nwt!

philrz avatar Apr 13 '25 18:04 philrz