Comments (10)
Think it could be a nice addition. By "allow for sorting only specific objects by keys" you mean to be able to recursively sort part of a object e.g. .a |= sort_keys
?
Some thoughts and questions:
- Could possibly
sory_by_keys
be an alternative name? . => {}
is fancytype == "object"
?- Is there some performance reason to split the input object into small one-key-objects and then merge them instead of doing something like
def sort_keys: walk(if . >= {} then . as $o | reduce keys[] as $k ({}; .[$k] = $o[$k]) end);
?
from jq.
Think it could be a nice addition. By "allow for sorting only specific objects by keys" you mean to be able to recursively sort part of a object e.g.
.a |= sort_keys
?
I like this too.
Some thoughts and questions:
* Could possibly `sory_by_keys` be an alternative name?
Since the existing command-line option is already --sort-keys
, naming the new builtin something close to that seems best, IMO.
* `. => {}` is fancy `type == "object"`?
Huh. In jq the greater-than-or-equal operator is >=
, not =>
. Comparisons of values of different types return the type of one minus the type of the other, with types expressed numerically. The only input value that would cause . >= {}
to be true is {}
.
* Is there some performance reason to split the input object into small one-key-objects and then merge them instead of doing something like `def sort_keys: walk(if . >= {} then . as $o | reduce keys[] as $k ({}; .[$k] = $o[$k]) end);`?
I would definitely implement this as a C-coded builtin in jq to optimize this. And probably just rewrite each object's insertion order by re-writing all the next
fields of all the buckets to match sorted key order.
from jq.
I should add that I wish keys
had been a special function (like empty
) that streams the object's keys or array's indices rather than outputting an array of keys. We should probably add a streamkeys
or keyss
or some such built-in that does just that. EDIT: Or maybe special syntax for this, like .[!]
(since Bash uses !
in ${!var[@]}
to refer to keys instead of values.
from jq.
Ah, so here . => {}
(or more likely, . >= {}
really means . != {}
, and assumes .
is an object.
from jq.
Yes sorry for my shitty typing, i meant . >= {}
from jq.
sort
does not sort recursively, map
does not map recursively, and neither should sort_keys
operate recursively.
Furthermore, a non-recursive sort_keys
(*) is perfectly useful in itself, and the recursive version of sort_keys
can be easily enough implemented using the non-recursive version, so adding the non-recursive version should be more than sufficient
-
This def has the semantics I have in mind:
def sort_keys: to_entries | sort | from_entries;
from jq.
Since the existing command-line option is already
--sort-keys
, naming the new builtin something close to that seems best, IMO.
Mm agree, that make sense. Also if we ever would wants a _by
variant it makes more sense sort_keys_by(f)
compared to sort_by_keys_by
😬
I would definitely implement this as a C-coded builtin in jq to optimize this. And probably just rewrite each object's insertion order by re-writing all the
next
fields of all the buckets to match sorted key order.
Also makes sense 👍
from jq.
sort does not sort recursively, map does not map recursively, and neither should sort_keys operate recursively.
That's a good point. But would be confusing that sort_keys
would be non-recursive but --sort-keys
would? Hmm
from jq.
sort does not sort recursively, map does not map recursively, and neither should sort_keys operate recursively.
That's a good point. But would be confusing that
sort_keys
would be non-recursive but--sort-keys
would? Hmm
Yes, I think so, but a non-recursive version is needed too.
from jq.
a non-recursive version is needed too.
Here's a thought: define sort_keys
non-recursively but in a way that
makes it trivial to use recursively, e.g. by walk(sort_keys)
.
An appropriate def would be:
def sort_keys:
if type == "object" then to_entries | sort | from_entries else . end;
Regarding the tension between having the command-line option
--sort-keys
be recursive but the builtin sort_keys
be
non-recursive -- if this is indeed going to be a significant obstacle,
then how about deprecating the long form --sort-keys
in favor of an
alternative long form name for the -S option?
from jq.
Related Issues (20)
- 1 test fails on WSL Debian HOT 14
- Version number pattern
- Base64d filter is not working as expected. HOT 2
- Allow user to control colors of int/bool/number HOT 2
- Incorrect results when subtracting two long numbers HOT 2
- make test failed under WSL, version jq 1.7.1-8-gf51e841 HOT 3
- Typo in the manual HOT 1
- try-catch + ascii_downcase fails in "catch" condition HOT 5
- Error suppression operator breaks function behaviour HOT 2
- Unrecognized HTML entity: `©` while running xq command HOT 1
- Setting non-existent elements of array slices uncovers hidden elements
- nan123 HOT 6
- Feature: Allow keys/array strings to be different color than object keys
- Modulo operator drops fractional part HOT 1
- `indices` behaves differently when dealing with arrays vs strings HOT 2
- Documentation Request: Include subsection links in documentation sidebar HOT 1
- Documentation Request: Include additional examples in the "Update assignment: |=" section HOT 2
- Stream delimiters in `fromstream`
- `make check` fails on `strftime` if the locale setting is not english. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jq.