Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort on value and nest it

What I want to have is a nested sort based on the weeknumber

This is an example of a array list that I want to sort.
I am using the lodash plugin.
‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎‏‏‎ ‎

[{
  "Pnl": 29.0035635,
  "date": "11/14/2022",
  "dayNumber": 1,
  "translationDayOfWeek": "Monday"
},
{
  "Pnl": 50.8878545,
  "date": "11/08/2022",
  "dayNumber": 2,
  "translationDayOfWeek": "Tuesday"
},
{
  "Pnl": 73.1014552,
  "date": "11/08/2022",
  "dayNumber": 2,
  "translationDayOfWeek": "Tuesday"
},
{
  "Pnl": 32.477,
  "date": "11/08/2022",
  "dayNumber": 6,
  "translationDayOfWeek": "Saturday"
},
{
  "Pnl": 25.43999561,
  "date": "09/30/2022",
  "dayNumber": 5,
  "translationDayOfWeek": "Friday"
},
{
  "Pnl": 17.6294068,
  "date": "09/30/2022",
  "dayNumber": 1,
  "translationDayOfWeek": "Monday"
}]

This is want I want for a output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[
  {
    "dayNumber": 1,
    "orders": [
      {
        "Pnl": 29.0035635,
        "date": "11/14/2022",
        "dayNumber": 1,
        "translationDayOfWeek": "Monday"
      },
      {
        "Pnl": 17.6294068,
        "date": "09/30/2022",
        "dayNumber": 1,
        "translationDayOfWeek": "Monday"
      }
    ]
  },
  {
    "dayNumber": 2,
    "orders": [
      {
        "Pnl": 50.8878545,
        "date": "11/08/2022",
        "dayNumber": 2,
        "translationDayOfWeek": "Tuesday"
      },
      {
        "Pnl": 73.1014552,
        "date": "11/08/2022",
        "dayNumber": 2,
        "translationDayOfWeek": "Tuesday"
      }
    ]
  }
]

I tried the solutions on stackoverflow post but it’s not the result I need.

>Solution :

If you are using loadsh,

const arr = []; //your array
_.map(
    _.groupBy(arr, function (obj) {return obj.dayNumber}),
    (order,index) => ({dayNumber: index, orders: order})
)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading