nushell icon indicating copy to clipboard operation
nushell copied to clipboard

Support sub-record keys and closures in `group-by`/`sort-by`

Open mon-jai opened this issue 2 years ago • 3 comments

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

mon-jai avatar Mar 05 '23 06:03 mon-jai

Interesting point!

For reference to the interested parties. In the nushell code base the "sub-record keys" are referred to as "cell paths".

sholderbach avatar Mar 06 '23 12:03 sholderbach

Noting additional interest in this issue, from the discord help channel

cablehead avatar Mar 13 '24 17:03 cablehead

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?

cablehead avatar Apr 24 '24 14:04 cablehead