0

How to sort separately 2 arrays of objects inside an array ? A solution with Lodash needed. Thank you.

Example of Array to sort by year:

var objects = [[{
      year: 2010,
      name: "john",
      value: 30
    },
    {
      year: 2009,
      name: "john",
      value: 40
    }
  ],
  [{
      year: 2018,
      name: "bob",
      value: 40
    },
    {
      year: 2015,
      name: "bob",
      value: 30
    }]]

Desired output after sorting by year:

[[{
      year: 2009,
      name: "john",
      value: 40
    },
    {
      year: 2010,
      name: "john",
      value: 30
    }
  ],
  [{
      year: 2015,
      name: "bob",
      value: 30
    },
    {
      year: 2018,
      name: "bob",
      value: 40
    }]]
1

4 Answers 4

2

orderBy on every sub collection should suffice

var objects = [
[{
year: 2010,
name: "john", 
value: 30
},
{
year: 2009,
name: "john",
value: 40
}],
[{
year: 2018,
name: "bob", 
value: 40
},
{
year: 2015,
name: "bob",
value: 30
}]
]

console.log(objects.map(subObject => _.orderBy(subObject, "year")));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

1

You can use map() on array of arrays and return the sorted array in map function.

var arr = [[{year:2010,name:"john",value:30},{year:2009,name:"john",value:40}],[{year:2018,name:"bob",value:40},{year:2015,name:"bob",value:30}]];


const res = arr.map(x => x.slice().sort((a,b) => a.year - b.year));
console.log(res)

5 Comments

why did you use slice ?
@AmitBaranes sort() modifies the original array. If i wouldn't have used slice() the origianal array whould have changed. Its just to make a copy of array.
Nice, didn't know that :)
@MaheerAli: I don't get it actually, coz when I do console.log(arr), it appears to me that arr remained the same while res is sorted. May I know how to prove what you're saying
@Isaac I am saying that if you don't use slice() then the original array i.e arr will be changed. In my code I am using slice() so arr remains same. Remove slice() you will see array is modified and becomes sorted.
0

You need to map the array and than sort it :

const objects = [
[{
year: 2010,
name: "john", 
value: 30
},
{
year: 2009,
name: "john",
value: 40
}],
[{
year: 2018,
name: "bob", 
value: 40
},
{
year: 2015,
name: "bob",
value: 30
}]
] 

const sorted = objects.map(r=>r.sort((a,b)=>a.year - b.year));

console.log(sorted)

Comments

0

You can generate a function with _.partialRight() and _.map() to _.sortBy() the sub arrays:

const { partialRight: pr, map, sortBy } = _;

const sortSubArrays = pr(map, arr => sortBy(arr, 'year'));

const objects = [[{year:2010,name:"john",value:30},{year:2009,name:"john",value:40}],[{year:2018,name:"bob",value:40},{year:2015,name:"bob",value:30}]];

const output = sortSubArrays(objects);

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: auto; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

Or use lodash/fp and drop the partialRight:

const { map, sortBy } = _;

const sortSubArrays = map(sortBy('year'));

const objects = [[{year:2010,name:"john",value:30},{year:2009,name:"john",value:40}],[{year:2018,name:"bob",value:40},{year:2015,name:"bob",value:30}]];

const output = sortSubArrays(objects);

console.log(output);
.as-console-wrapper { max-height: 100% !important; top: auto; }
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.