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
    }]]
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
troplost
  • 23
  • 5
  • 2
    Possible duplicate of [Sort array of objects by string property value](https://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value) – Webber May 12 '19 at 10:11

4 Answers4

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>
Krzysztof Krzeszewski
  • 5,912
  • 2
  • 17
  • 30
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)
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • 1
    why did you use slice ? – Amit Baranes May 12 '19 at 10:19
  • 1
    @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. – Maheer Ali May 12 '19 at 10:21
  • Nice, didn't know that :) – Amit Baranes May 12 '19 at 10:32
  • @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 May 12 '19 at 10:58
  • @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. – Maheer Ali May 12 '19 at 14:23
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)
Amit Baranes
  • 7,398
  • 2
  • 31
  • 53
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>
Ori Drori
  • 183,571
  • 29
  • 224
  • 209