Javascript sort object key indexes

here is my structure/example:

let arrWeekdays = ['Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'];

let obj1 = {
 'Montag' : ['test string'],
 'Dienstag' : ['test string'],
 'Mittwoch' : ['test string'],
 'Donnerstag' : ['test string'],
 'Freitag' : ['test string'],
 'Samstag' : ['test string']
}

let obj2 = {
 'Donnerstag' : ['test string'],
 'Samstag' : ['test string'],
 'Montag' : ['test string'],
 'Dienstag' : ['test string'],
 'Mittwoch' : ['test string'],
 'Freitag' : ['test string']
}


for ( key in objWeekdays ) {
  // do something
}

"objectWeekdays" in for in loop is sometimes ordered object as "obj1", but sometimes also unordered as "obj2".

My problem:

for in with obj1 => key index of "Montag" is 0

for in with obj2 => key index of "Montag" is 2

Is there any way to sort Object keys before "for in" loop?

I would like always to start with "Montag" in for in loop.

Thanks in advance for any advice or help.javascri

15 thoughts on “Javascript sort object key indexes”

Leave a Comment