Ramda sort array of nested object

Saurabh Agrawal

I am looking to sort an array of nested object using Ramda. I have an array:

const people = [
  { name: 'Emma', data:{ age: 70 }},
  { name: 'Peter', data:{  age: 78 }},
  { name: 'Mikhail', data:{  age: 62 }},
];

I want to sort above array using Ramda. I got this but it does not work for me.

Your help will be highly appreciated.

Nick Parsons

As you want to sort by the path data.age, you can get the prop data from your object using R.prop which will give you an object, and then use R.prop() again on that object to get the age property. To make a function which does this, you can use R.compose():

const byAge = R.ascend(R.compose(R.prop('age'), R.prop('data')));
const people = [
  { name: 'Emma', data:{ age: 70 }},
  { name: 'Peter', data:{  age: 78 }},
  { name: 'Mikhail', data:{  age: 62 }},
];

const peopleByYoungestFirst = R.sort(byAge, people);
console.log(peopleByYoungestFirst);
//=> [{"name":"Mikhail","data":{"age":62}},{"name":"Emma","data":{"age":70}},{"name":"Peter","data":{"age":78}}]
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js" integrity="sha256-buL0byPvI/XRDFscnSc/e0q+sLA65O9y+rbF+0O/4FE=" crossorigin="anonymous"></script>

To break down the above compose function, say you have the object obj, which is:

obj = { name: 'Emma', data:{ age: 70 }}

Performing R.prop('data')(obj) will give:

{ age: 70 }

As you are interested in the age property of the returned object, you again can run R.prop('age') on the above object:

R.prop('age')({age: 70})

This will give 70. So above line can be written as:

R.prop('age')(R.prop('data')(obj))
^^^^^ f ^^^^^ ^^^^^^ g ^^^^^  ^ x

The issue with this function, however, is that it doesn't return a function which we can pass obj into to get 70 out of. Currently, it takes the form of f(g(x)), by composing it, we can get the form of (f.g)(x), where f.g composes the functions f and g to produce a new function. To compose in Ramda, we can use R.compose():

R.compose(R.prop('age'), R.prop('data'))(obj)

This can be eta-reduced to remove the obj like it is in the example.

This approach can be generalized to:

const {pipe, split, reverse, map, apply, compose, ascend} = R;
const makeSortFn = compose(ascend, pipe(split('.'), reverse, map(R.prop), apply(compose)));

const byAge = makeSortFn('data.age');
const people = [
  { name: 'Emma', data:{ age: 70 }},
  { name: 'Peter', data:{  age: 78 }},
  { name: 'Mikhail', data:{  age: 62 }},
];

const peopleByYoungestFirst = R.sort(byAge, people);
console.log(peopleByYoungestFirst);
//=> [{"name":"Mikhail","data":{"age":62}},{"name":"Emma","data":{"age":70}},{"name":"Peter","data":{"age":78}}]
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js" integrity="sha256-buL0byPvI/XRDFscnSc/e0q+sLA65O9y+rbF+0O/4FE=" crossorigin="anonymous"></script>

But instead, I would favour Ori's approach, which can be genralized much easier by splitting a string:

R.sortBy(R.path(path_str.split('.')))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Javascript

Transform array of objects to nested objects using Ramda

From Java

Sort array of objects by one property of nested object

From Dev

Ramda: How to filter based on a value in a nested array

From Dev

How to sort an array by the value of an object within a nested array

From Dev

How to sort an array of objects based on nested key using Ramda?

From Dev

Ramda `evolve` nested object

From Dev

Ramda: Delete object in nested structure

From Dev

change an object property in array with ramda

From Dev

How do you sort an array of words with Ramda?

From Javascript

Ramda - Convert array of objects into object

From Dev

Sort nested array of object in javascript

From Dev

Sort array of objects by nested object property

From Dev

JavaScript sort array descending by nested object

From Dev

Find an object by id in array Ramda

From Dev

Insert a new object to array Ramda

From Dev

Mongoose sort nested array of object not working

From Dev

How to sort nested Firebase DB's nested object array

From Dev

Sort array based on nested object value

From Dev

Sort From Nested Object Array MongoDB - NodeJS

From Dev

PHP sort nested array object

From Dev

Find object with given id in nested arrays with ramda, get all properties except children array

From Dev

Flatten and sort nested array of object by its array value

From Dev

Flatten and sort a nested JSON object into an array in javascript

From Dev

Ramda - how to add new properties to nested object

From Dev

Ramda - how to get nested array values

From Dev

How to filter records in a nested array using ramda?

From Dev

Ramda - How to sort array with nested object with string to number conversion

From Dev

Use Ramda to change deep nested object value

From Dev

Angular : Sort a nested list in an object in array

Related Related

  1. 1

    Transform array of objects to nested objects using Ramda

  2. 2

    Sort array of objects by one property of nested object

  3. 3

    Ramda: How to filter based on a value in a nested array

  4. 4

    How to sort an array by the value of an object within a nested array

  5. 5

    How to sort an array of objects based on nested key using Ramda?

  6. 6

    Ramda `evolve` nested object

  7. 7

    Ramda: Delete object in nested structure

  8. 8

    change an object property in array with ramda

  9. 9

    How do you sort an array of words with Ramda?

  10. 10

    Ramda - Convert array of objects into object

  11. 11

    Sort nested array of object in javascript

  12. 12

    Sort array of objects by nested object property

  13. 13

    JavaScript sort array descending by nested object

  14. 14

    Find an object by id in array Ramda

  15. 15

    Insert a new object to array Ramda

  16. 16

    Mongoose sort nested array of object not working

  17. 17

    How to sort nested Firebase DB's nested object array

  18. 18

    Sort array based on nested object value

  19. 19

    Sort From Nested Object Array MongoDB - NodeJS

  20. 20

    PHP sort nested array object

  21. 21

    Find object with given id in nested arrays with ramda, get all properties except children array

  22. 22

    Flatten and sort nested array of object by its array value

  23. 23

    Flatten and sort a nested JSON object into an array in javascript

  24. 24

    Ramda - how to add new properties to nested object

  25. 25

    Ramda - how to get nested array values

  26. 26

    How to filter records in a nested array using ramda?

  27. 27

    Ramda - How to sort array with nested object with string to number conversion

  28. 28

    Use Ramda to change deep nested object value

  29. 29

    Angular : Sort a nested list in an object in array

HotTag

Archive