MASON-INTEREST-L Archives

January 2011

MASON-INTEREST-L@LISTSERV.GMU.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Martin Pokropp <[log in to unmask]>
Reply To:
MASON Multiagent Simulation Toolkit <[log in to unmask]>
Date:
Sat, 8 Jan 2011 01:17:39 +0100
Content-Type:
text/plain
Parts/Attachments:
text/plain (54 lines)
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

ATOM RSS1 RSS2