Subject: | |
From: | |
Reply To: | |
Date: | Sat, 8 Jan 2011 01:17:39 +0100 |
Content-Type: | text/plain |
Parts/Attachments: |
|
|
A solution to your problem could be the following:
steppableAgents.sort(new java.util.Comparator()
{
public int compare(Object o1, Object o2)
{
SteppableAgent a1 = (SteppableAgent)o1;
SteppableAgent a2 = (SteppableAgent)o2;
double ret = a1.diff - a2.diff;
if (ret > 0) return -1;
else if (ret < 0) return 1;
else return 0;
}
});
This way your agents should be sorted in descending order. If you
want it ascending, change -1 to 1 and 1 to -1.
Hope this helps!
Martin
Am 07.01.2011 um 21:51 schrieb Makowsky, Michael D.:
> I am unclear, due to my limited java abilities, how to sort a bag
> of steppable agents by an attribute. Specifically. I have a bag of
> agents that I would like to sort by a value stored as a double
> (public double diff). The documentation states that I need to
> provide a comparator:
>
> sort
> public void sort(java.util.Comparator c)
>
> but am uncertain how to provide the java.lang.Double.compare method
> such that it sorts the bag of objects by the "diff" attribute, if
> in fact that is what I need to do.
> Thank you for your assistance.
>
> Michael D. Makowsky
|
|
|