So i tried to apply bubble sort technique to an associative array.
What i tried is making a normal array and then applying bubble sort. This worked , so now I'm trying to do the same for my associative array but I can't understand why it doesn't work, can someone explain and tell me how to do this?
Normal Array bubble sort code: <-- This one works
var numbers= new Array()
numbers[0] = 22;
numbers[1] = 3;
numbers[2] = 65;
numbers[3] = 75;
numbers[4] = 500;
numbers[5] = 2;
numbers[6] = 44;
for(var i=0; i<numbers.length; i++)
{
if(numbers[i] < numbers[i+1])
{
var tempGetal = numbers[i];
numbers[i] = numbers[i+1];
numbers[i+1] = tempGetal;
}
}
console.log("Smallest number from array is " + tempGetal);
associative array bubble sort code: <-- Doesn't work
var celsius= new Array()
celsius["Monday"] = 22;
celsius["Tuesday"] = 3;
celsius["Wednesday"] = 65;
celsius["Thursday"] = 75;
celsius["Friday"] = 1;
celsius["Saterday"] = 2;
celsius["Sunday"] = 44;
for(var temp in celsius)
{
if(celsius[temp] < celsius[temp+1])
{
var tempGetal = celsius[temp];
celsius[temp] = celsius[temp+1];
celsius[temp+1] = tempGetal;
}
}
console.log("Smallest number from this array is " + tempGetal[temp]);
Can anyone tell me if the method I'm trying to apply is possible?
Thanks in advance!