Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: sort_by() doesn't mention sorting by multiple keys of an object #2467

Closed
jbrains opened this issue Aug 24, 2022 · 4 comments · Fixed by #2744
Closed

Docs: sort_by() doesn't mention sorting by multiple keys of an object #2467

jbrains opened this issue Aug 24, 2022 · 4 comments · Fixed by #2744
Labels

Comments

@jbrains
Copy link
Contributor

jbrains commented Aug 24, 2022

I wanted to sort an array of objects by multiple keys (to break ties in the first key). The Manual doesn't describe this at all, but a Stack Overflow article showed that it works with sort_by(key1, key2, ...).

I propose to add this to the Manual.

Based on a comment from #2466 I'm just going to submit a PR and wait patiently until someone merges it. No rush, since anyone who needs to know this will almost certainly eventually find the information.

@wader
Copy link
Member

wader commented Aug 24, 2022

Great addition i think. I didn't know about it and based on the test cases when it was added 11965aa it is intentional. Kind of neat. Also the implementation is neat, the output from the filter is collected in an array [f] so the compare will be between to arrays.

@sergeysosivio
Copy link

im against var agrs.
i want better to have additional sort_by/2, where second parameter can define isDescending order

chaining those sort_by/2 with pipe can create efficient multi-sorting pipeline, without extra use of reverse|sort_by|reverse for not breaking stablesort

@ratijas
Copy link

ratijas commented Nov 13, 2022

Interesting. So in my case of parsing kscreen-doctor --json list of outputs (screens, monitors in human-speak) I has to do this to sort them first by enabled key and then by id:

outputs=(${(f)"$(
    kscreen-doctor --json |
    jq --raw-output '
        .outputs |
        sort_by([.connected | not], [.name]) |
        .[] |
        [.id, .name, .connected] |
        .[]
    ')"})

I'm a bit surprised that it didn't work as sort_by([.connected | not, .name]) without separate arrays, and I don't fully understand what is going on. But, hey, it works!

@wader
Copy link
Member

wader commented Nov 15, 2022

I'm a bit surprised that it didn't work as sort_by([.connected | not, .name]) without separate arrays, and I don't fully understand what is going on. But, hey, it works!

Hi, you might want (.connected | not), .name else .name will index whatever .connected is.

$ jq -nc '{"connected": {"name": "b"}} | [.connected | not, .name]'
[false,"b"]

(but if .connected is bool i think you should have ende up with the error Cannot index boolean with string "name")

@itchyny itchyny added the docs label Jun 3, 2023
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#2169, close jqlang#1148)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#2169, close jqlang#1148)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#2169, close jqlang#1148)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 22, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 23, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 23, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 23, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 23, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 23, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 23, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
itchyny added a commit to itchyny/jq that referenced this issue Jul 23, 2023
- Add error/0 and mentions null input behavior (close jqlang#2231)
- Explain value iterator suffix syntax .foo[] (close jqlang#1047)
- Mention array slicing is also zero-based (close jqlang#2094)
- Add examples of input and inputs filters (close jqlang#2216, close jqlang#2470)
- Improve sort_by about multiple values (close jqlang#2103, close jqlang#2467, close jqlang#2474)
- Improve foreach section and simplify examples (close jqlang#1148, close jqlang#2169)
- Fix recurse/1 document on how it is identical using recurse/2 (close jqlang#2036, close jqlang#2412)
- Add non-string examples of index/1, rindex/1 (close jqlang#1422)
- Simplify the example of truncate_stream/1 (close jqlang#1736)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
5 participants