Copy link to clipboard
Copied
I've got an array like that
var tideArray = new Array();
tideArray.push({tide:"pmm", difference: -2});
tideArray.push({tide:"pms", difference: 5});
tideArray.push({tide:"bmm", difference: -7});
tideArray.push({tide:"bms", difference: 8});
tideArray.sortOn("difference", Array.NUMERIC);
trace(tideArray[0].tide);
For now, it's choosing the minimum number (-7
) but I'd like to choose the closest number to 0
.
Is there a way to do that ?
I don't think sort or sortOn will do what you want. Here's a little method that will return the index of the array element closes tto zero:
...var tideArray = new Array();
tideArray.push({tide:"pmm", difference: -2});
tideArray.push({tide:"pms", difference: 5});
tideArray.push({tide:"bmm", difference: -7});
tideArray.push({tide:"bms", difference: 8});
trace(closestToZero(tideArray));
function closestToZero(a:Array):int
{
var curDelta:int = Math.abs(0 - a[0].difference);
var curIndex:int = 0;
for(var i:in
Copy link to clipboard
Copied
I don't think sort or sortOn will do what you want. Here's a little method that will return the index of the array element closes tto zero:
var tideArray = new Array();
tideArray.push({tide:"pmm", difference: -2});
tideArray.push({tide:"pms", difference: 5});
tideArray.push({tide:"bmm", difference: -7});
tideArray.push({tide:"bms", difference: 8});
trace(closestToZero(tideArray));
function closestToZero(a:Array):int
{
var curDelta:int = Math.abs(0 - a[0].difference);
var curIndex:int = 0;
for(var i:int = 1; i < a.length; i++){
var thisDelta:int = Math.abs(0 - a.difference);
if(thisDelta < curDelta){
curIndex = i;
}
}
return curIndex;
}
Copy link to clipboard
Copied
Hmm not sure that is working...
I've tested and it returns "1"
If I change the values, for example to :
tideArray.push({tide:"pmm", difference: 3});
tideArray.push({tide:"pms", difference: -4});
tideArray.push({tide:"bmm", difference: 10});
tideArray.push({tide:"bms", difference: 8});
trace(closestToZero(tideArray)); //// output 0 (it should output 3)
Also, is it possible to output the name of the array ? (in this example, I'd like to output "pmm" because it's the closest to 0)
Copy link to clipboard
Copied
The output is correct - it's the array index not the value...
If you want the value:
var i:int = closestToZero(tideArray);
trace(tideArray.difference);
And if you want name too:
trace(tideArray.name, tideArray.difference);
Copy link to clipboard
Copied
Oh sorry , my bad. I read to quickly ! Thanks !!!
Copy link to clipboard
Copied
Hmm, in fact, I don't know why but sometimes the results are wrong.
I've tried this :
var tideArray = new Array();
tideArray.push({tide:"haute", difference: "-14"});
tideArray.push({tide:"haute", difference: "-3"});
tideArray.push({tide:"basse", difference: "-9"});
tideArray.push({tide:"basse", difference: "4"});
trace(closestToZero(tideArray));function closestToZero(a:Array):int {
var curDelta:int = Math.abs(0 - a[0].difference);
var curIndex:int = 0;
for(var i:int = 1; i < a.length; i++){
var thisDelta:int = Math.abs(0 - a.difference);
if(thisDelta < curDelta){
curIndex = i;
}
}
return curIndex;
}
The output is 3 (so the 4th array that is equal to 4). But, the closest to 0 is not the fourth one, but the second one (-3)..
Do you know why the code make this mistake ?
Copy link to clipboard
Copied
Yes... not sure how but this line is missing:
curDelta = thisDelta;
that goes inside the if statement where curIndex is set to i - so: