I make a schedule as a PWA. Right now the classes are displaying well (miraculously) but we are in a week that meets a change of month. So my Monday classes are posted on Thursday. Currently, the sorting of the objects is in ascending order without taking into account the month: [1,2,3,27,28]
whereas I seek to have that: [27,28,1,2,3]
. How do I sort considering the month change?
This is what the JSON looks like:
'27': [
{
day: 27,
isLesson: true,
start: '16:00',
end: '17:00',
room: 'myRoom',
prof: 'myTeacher',
spe: 'MySpe'
},
{
day: 27,
isLesson: true,
start: '14:15',
end: '15:45',
room: 'myRoom',
prof: 'myTeacher',
spe: 'MySpe'
},
{
day: 27,
isLesson: true,
start: '13:45',
end: '14:15',
room: 'myRoom',
prof: 'myTeacher',
spe: 'MySpe'
}
],
'28': [
{
day: 28,
isLesson: true,
start: '09:00',
end: '10:00',
room: 'myRoom',
prof: 'myTeacher',
spe: 'MySpe'
},
{ isLesson: false, start: '10:00', end: '13:45' },
{
day: 28,
isLesson: true,
start: '13:45',
end: '15:45',
room: 'myRoom',
prof: 'myTeacher',
spe: 'MySpe'
},
Currently my code to arrange the json and send it for display looks like this:
function arrange(json) {
let new_json = {};
let previousSession = {};
for (const [uid, session] of Object.entries(json)) {
if (!uid.match(/^[d-]*$/))
{
continue;
}
let minMidi = new Date(session.start);
minMidi.setHours(11, 15, 0);
let maxMidi = new Date(session.start);
maxMidi.setHours(12, 45, 0);
let session_json = {};
session_json.day = new Date(session.dtstamp).getDate();
if (previousSession.end && minMidi >= previousSession.end && previousSession.start && maxMidi <= session.start)
{
let pause_json = {};
pause_json.isLesson = false;
pause_json.start = previousSession.end.toLocaleTimeString('fr-FR', localStringOpt);
pause_json.end = session.start.toLocaleTimeString('fr-FR', localStringOpt);
new_json[session_json.day] = new_json[session_json.day] ?? [];
new_json[session_json.day].push(pause_json);
}
session_json.isLesson = true;
const start = new Date(session.start);
previousSession.start = start;
session_json.start = start.toLocaleTimeString('fr-FR', localStringOpt);
const end = new Date(session.end);
previousSession.end = end;
session_json.end = end.toLocaleTimeString('fr-FR', localStringOpt);
//[...]
new_json[session_json.day] = new_json[session_json.day] ?? [];
new_json[session_json.day].push(session_json);
}
console.log(new_json);
return Object.values(new_json);
}
I have done several tests but I don’t understand. I can’t get the order right
2
Answers
Alphabetic or alphanumeric (with at least one alphabetic character) object property names appear in insertion order during iteration. However, purely numeric property names will always appear before any non-numeric property names, in numerical order, regardless of insertion order.
You could override the iterator method for the array in order to enforce your own property order during iteration, but it’s much easier to just use a Map instead. Maps will observe insertion order during iteration, whether key names are numeric or not.
JavaScrip Objects can’t sort the keys, but if you need to sort your object by the key you have to transform it to a array. This array can be sorted as every other array.
In the following example we create a
[key, value]
tuple from our object and this can be sorted: