0

I need to sort array of object by 3 criteria:

  1. Time (earliest)
  2. Role (a-z)
  3. User (a-z)

So first I need to sort them out by the start date, then that sorted array I need to kinda make groups of roles and sort them alphabetically (but still maintain sorting by time), and in the end, if the shifts let say are the same role and start at the same time sort them by user name.

Here is a simplified object

const array = [{
    role: "Host"
    start: "2022-08-08T02:30:00+00:00"
    user: "Joe Doe"
}]

Maybe is best to start off by sorting out the roles first, and that small group of roles sort by the time or I don't know, if you have any ideas I would appreciate any suggestion, Thanks!!

Ok so I guess you need a bit more data: Here is initial array

const array = [
{
    role: "Host"
    start: "2022-08-08T02:30:00+00:00"
    user: "AJoe Doe"
},
{
    role: "Cook"
    start: "2022-08-08T00:30:00+00:00"
    user: "DJoe Doe"
},
{
    role: "Host"
    start: "2022-08-07T23:30:00+00:00"
    user: "CJoe Doe"
},
{
    role: "Host"
    start: "2022-08-07T23:30:00+00:00"
    user: "BJoe Doe"
},
{
    role: "Cook"
    start: "2022-08-08T02:30:00+00:00"
    user: "EJoe Doe"
}]

And here is what I need to get (sorted out by the role, time and in the end by user:

const array = [
{
    role: "Cook"
    start: "2022-08-08T00:30:00+00:00"
    user: "DJoe Doe"
},
{
    role: "Cook"
    start: "2022-08-08T02:30:00+00:00"
    user: "EJoe Doe"
},
{
    role: "Host"
    start: "2022-08-08T02:30:00+00:00"
    user: "AJoe Doe"
},
{
    role: "Host"
    start: "2022-08-07T23:30:00+00:00"
    user: "BJoe Doe"
},
{
    role: "Host"
    start: "2022-08-07T23:30:00+00:00"
    user: "CJoe Doe"
}]
Mario Rozic
  • 240
  • 2
  • 14
  • Please provided sample data that is less trivial (multiple entries, with some the same time or role, ...etc), and provide the expected result for that sample. It is not clear whether you mean with "group" that the output should have some group-structure. So an example should clarify all that. – trincot Aug 08 '22 at 09:41
  • @trincot Ok I added an example. – Mario Rozic Aug 08 '22 at 09:51
  • @pilchard Nah, I had 2 criteria and I did all that, but now I have one more and I don't know where to start – Mario Rozic Aug 08 '22 at 09:53
  • 1
    @MarioRozic, it is the same principle. If you could not make it work as explained in that link (read **all** the answers there -- they include code that works for more than two properties), then please edit your question and show your attempt, and explain where it goes wrong. – trincot Aug 08 '22 at 09:54
  • @trincot ok, so I got it, i managed to sort by the role and start, thanks, that was actually very helpful, but now I have to sort same shifts and same time by its name, how do you propose to do that? – Mario Rozic Aug 08 '22 at 11:22
  • That is always the same principle... – trincot Aug 08 '22 at 11:24

0 Answers0