0

I've got this groupsArray which I want to sort based on the days which are closest to 1 and the times
start and the end should be the earliest ones... I've tried a lot of stuff but I was unable to write a correct code to work... I don't want to sort classtimes, I want to sort groupsArray based on classtimes

const groupsArray = [
    {
        classtimes: [
            {
                day: 4,
                start: { hour: 21, minute: 0 },
                end: { hour: 23, minute: 0 },
            },
            {
                day: 3,
                start: { hour: 17, minute: 0 },
                end: { hour: 20, minute: 0 },
            },
        ],
        name: "6th Group",
    },
    {
        classtimes: [
            {
                day: 1,
                start: { hour: 18, minute: 0 },
                end: { hour: 20, minute: 0 },
            },
            {
                day: 1,
                start: { hour: 14, minute: 0 },
                end: { hour: 16, minute: 40 },
            },
            {
                day: 2,
                start: { hour: 16, minute: 0 },
                end: { hour: 18, minute: 40 },
            },
        ],
        name: "5th Group",
    },
];

I was thinking of writing something like this but It didn't work out...

groupsArray .sort((a,b) => a.classTimes.forEach(e => e.day > b.classTimes))
jetski
  • 182
  • 2
  • 16
  • please add the wanted result. – Nina Scholz Mar 16 '21 at 16:05
  • You have a typo in your code. It's `classtimes` not `classTimes` – pew007 Mar 16 '21 at 16:07
  • the first version was the result, now I've edited the code so the second object (5th Group) should be the first one based the classTimes – jetski Mar 16 '21 at 16:07
  • Does this answer your question? [Sorting an array of objects by property values](https://stackoverflow.com/questions/979256/sorting-an-array-of-objects-by-property-values) – Dexygen Mar 16 '21 at 16:24

1 Answers1

0

You could get the minimum minutes from classtimes and sort by this value.

const
    groupsArray = [{ classtimes: [{ day: 4, start: { hour: 21, minute: 0 }, end: { hour: 23, minute: 0 } }, { day: 3, start: { hour: 17, minute: 0 }, end: { hour: 20, minute: 0 } }], name: "6th Group" }, { classtimes: [{ day: 1, start: { hour: 18, minute: 0 }, end: { hour: 20, minute: 0 } }, { day: 1, start: { hour: 14, minute: 0 }, end: { hour: 16, minute: 40 } }, { day: 2, start: { hour: 16, minute: 0 }, end: { hour: 18, minute: 40 } }], name: "5th Group" }];

groupsArray.sort((a, b) => {
    const
        minutes = ({ day, start: { hour, minute } }) => day * 1440 + hour * 60 + minute,
        getMin = array => array.reduce((min, o) => Math.min(min, minutes(o)), Number.MAX_VALUE);

    return getMin(a.classtimes) - getMin(b.classtimes);
});

console.log(groupsArray);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Yeah but the ones that the have the same day should be sorted depended on start and end hour after – jetski Mar 16 '21 at 16:13