Looking to organize an array consisting of sets or pairs based on the first number in each pair.
Here is the code snippet:
function randomMap(numOfHills){
for(var i=0; i<numOfHills; i++){
xRandomForHills = Math.floor((Math.random() * 2000));
if(xRandomForHills >= 1700){
xRandomForHills = xRandomForHills - 300;
}
if(xRandomForHills <= 300){
xRandomForHills = xRandomForHills + 300;
}
yRandomForHills = Math.floor((Math.random() * 300));
if(yRandomForHills <= 100){
yRandomForHills = yRandomForHills + 300;
}
hillArray.push([xRandomForHills,yRandomForHills]);
console.log("my Hill = " + hillArray[i]);
}
sortArray(numOfHills);
};
function sortArray(numOfHills){
hillArray.sort(function(a, b){return b-a});
hillArray;
for(var i=0; i<numOfHills; i++){
console.log("Sorted = " + hillArray[i]);
}
drawMap();
};
Debug information:
my Hill = 917,136
my Hill = 885,274
my Hill = 876,347
my Hill = 1011,184
my Hill = 563,273
Sorted = 917,136
Sorted = 885,274
Sorted = 876,347
Sorted = 1011,184
Sorted = 563,273
Desired sorting of the first part of the array:
my Hill = 917,136
my Hill = 885,274
my Hill = 876,347
my Hill = 1011,184
my Hill = 563,273
Sorted = 563,273
Sorted = 876,347
Sorted = 885,274
Sorted = 917,136
Sorted = 1011,184