Reputation: 145
I have the following function below:
function GeraHorario(inicio,fim,tempoConsulta)
{
var agenda = []
var data = new Date(2016, 0, 1, inicio, 0, 0, 0)
var horainicial = inicio
while (data.getDate() == 1) {
if(data.getHours() < fim)
{
var horario = data.toLocaleTimeString('pt-BR');
data.setMinutes(data.getMinutes()+tempoConsulta);
var d1 = horario.replace(/(.*)\D\d+/, '$1');
agenda.push(d1);
}else{break}
}
console.log(agenda)
return agenda
}
This function generates this array
["09:00", "09:10", "09:20", "09:30", "09:40", "09:50", "10:00", "10:10", "10:20", "10:30", "10:40", "10:50", "11:00", "11:10", "11:20", "11:30", "11:40", "11:50", "12:00", "12:10", "12:20", "12:30", "12:40", "12:50", "13:00", "13:10", "13:20", "13:30", "13:40", "13:50", "14:00", "14:10", "14:20", "14:30", "14:40", "14:50", "15:00", "15:10", "15:20", "15:30", "15:40", "15:50", "16:00", "16:10", "16:20", "16:30", "16:40", "16:50", "17:00", "17:10", "17:20", "17:30", "17:40", "17:50", "18:00", "18:10", "18:20", "18:30", "18:40", "18:50"]
I make a map in the array and it looks like this. This is the function used in map
function GroupHoras(array)
{
var result = array.reduce(function( map, hourStr ) {
var hour = hourStr.charAt(0) + hourStr.charAt(1);
if (!map.hasOwnProperty(hour)) map[hour] = [];
map[hour].push(hourStr);
return map;
}, {});
return result
}
It looks like after the map
Object {10: Array[6], 11: Array[6], 12: Array[6], 13: Array[6], 14: Array[6], 15: Array[6], 16: Array[6], 17: Array[6], 18: Array[6], 09: Array[6]}
However as you can see, the value 09 should be at the beginning, go to the end of the array of objects. How do I ordernar this subject increasingly?
Upvotes: 0
Views: 88
Reputation: 658
Javascript does not guarantee key order in maps. See this answer for quotes from the spec: Does JavaScript Guarantee Object Property Order?.
However there are ways to get around this, the second answer to this question shows how you can use JSON.sringify() to ensure proper ordering: Sort JavaScript object by key
Upvotes: 0
Reputation: 680
Javascript object arrays are not sortable objects. Every browser will show them in different orders even with the same set of data.
What you can do is to create a two dimensional JS array and the first index elements will be the hours and the second index will be a one-dimensional array which, I suppose, you keep the items in that hour.
So additionally to your second function you should be able to use the following codes to create your own sorting function and get a two-dimensional array instead of using a JS object array.
var arr = new Array();
for(var hour in map){
var arr2 = new Array();
arr2.push(hour, map[hour]);
arr.push(arr2);
}
arr.sort();//After this you will have a sorted array
Upvotes: 0
Reputation: 136104
You cannot "order" the properties of a javascript object.
Perhaps a good workaround is to store an an array containing the property names in the order you want to display them, and use that for the purpose of ordering:
var order = ["09","10","11"]
var obj = {"10":"ten","11":"Eleven", "09": "Nine"};
for(var i=0;i<order.length;i++){
var prop = order[i];
console.log(prop,obj[prop]);
}
Upvotes: 2