sort-on

 

sort-on allows us to sort the agents in an agentset based on a reporter such as an agent characteristic (e.g.,size, xcor) or custom agent variable (e.g., age, speed). Its syntax is: sort-on [ variable-or-reporter ] agentset. For example, if we wanted to create a model of earthquake search-and-rescue in which the rescuers checked the largest buildings first, we would write the following code:

let buildings-sorted sort-on [size] buildings
ask rescuers [
    move-to last buildings-sorted
]

Things to keep in mind when using sort-on:

 

Try it Yourself

 
 
 
 
 
 
 

What's next?

Once you mastered the sort-on primitive, don't stop there. Check out the resources below to improve your NetLogo skills.

 
Published NetLogo models that use the sort-on primitive:
 
 
Similar primitives:
sort-by

Reports a sorted version of a provided list based on a user-defined comparison.

Read more
count

Counts the number of agents in an agentset.

Read more
mean

Reports the average of a provided list of numerical values.

Read more
ask

Asks agents to do things.

Read more
 
Learn another primitive: