Support sub-record keys and closures in `group-by`/`sort-by`
Related problem
It is not easy to group/sort data with sub-keys.
Consider the following data structure storing Twitter tweets.
let tweets = [
{
"author": {
"id": 1,
"username": "mon-jai"
},
"created": 1677877558
"caption": "Hello world",
"comments": [{}, {}, {}]
},
# ...
]
In today's Nushell,
What if I want to group tweets by author?
$tweets | each {|$it| insert author_username { $it | get author.username } } | group-by author_username
What if I want to sort tweets by comments count?
$tweets | each {|$it| insert comments_count { $it | get comments | length } } | sort-by comments_count
The solution requires us to process the data beforehand and is not straight forward.
Describe the solution you'd like
By supporting sub-record keys and closures in group-by/sort-by, we can process the data more conveniently.
Sub-record keys
$tweets | group-by author.username
Closures
$tweets | sort-by { get comments | length }
Describe alternatives you've considered
No response
Additional context and details
No response
Interesting point!
For reference to the interested parties. In the nushell code base the "sub-record keys" are referred to as "cell paths".
Noting additional interest in this issue, from the discord help channel
From Steve on discord:
How do i best sort a list of strings by length? I came up with:
echo [Steve,Alice,Charles, Bob] | each { |x| [[name,len]; [$x ,($x | str length)]] } | flatten | sort-by len | get name
but that's a bit unwieldy. Can sort-by somehow take a closure?