Skip to main content
Trocergian
Known Participant
May 6, 2009
Answered

Theory on how to best compare objects.

  • May 6, 2009
  • 1 reply
  • 650 views

I'm soliciting thoughts on how best to approach this:

I have four objects with a .speed property. I want to compare all four and snag the object with the lowest speed. That alone I can get a grip on no problem. But, it's quite possible that 2 or more of the objects could have the same lowest speed. Here's where my ideas start to get muddled and rather involved. I'm hoping someone might help me cut through the fog and point me in the general direction of how I should be approaching this.

Here's my general thought at this point...

Compare two, toss the lowest in an array, if equal toss them both in.

Compare the third to the array, if it's lower replace the existing item(s) with this one, if it's equal add it.

Do a similiar thing with the fourth.

To do all that I can't think of anything but a boatload of IFs and nested IFs.

Does anyone have any other/better thoughts?

This topic has been closed for replies.
Correct answer

You could toss them all into an Array and use:

myArray.sortOn("speed", Array.NUMERIC);

The lowest speed should then be myArray[0];

1 reply

Correct answer
May 6, 2009

You could toss them all into an Array and use:

myArray.sortOn("speed", Array.NUMERIC);

The lowest speed should then be myArray[0];

Trocergian
Known Participant
May 6, 2009

Ooh! I like that!  Then I can just run a check to see if [0] = [1] and go from there.

Nice. Thanks a lot!

May 6, 2009

You're welcome.