Skip to content

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

@mon-jai

Description

@mon-jai

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

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions