Home
Learn NetLogo
What is NetLogo? Tutorial #0 Sample Model Tutorial #1 Models Tutorial #2 Commands Tutorial #3 Procedures
Documentation
NetLogo Dictionary Interface Guide Interface Tab Guide Info Tab Guide Code Tab Guide Programming Guide Transition Guide Preferences Guide Version History
Advanced Tools
Extension Manager Shapes Editor BehaviorSpace System Dynamics HubNet HubNet Authoring Logging Controlling Mathematica Link NetLogo 3D NetLogoLab Cluster Computing (HPC)
Extensions
Extensions Guide Arduino Array Bitmap CSV GIS GoGo LevelSpace Matrix Networks Palette Profiler Python Resource Rnd Sound Simple R Table Time Vid View2.5D
FAQ
Home
Learn NetLogo
What is NetLogo? Tutorial #0 Sample Model Tutorial #1 Models Tutorial #2 Commands Tutorial #3 Procedures
Documentation
NetLogo Dictionary Interface Guide Interface Tab Guide Info Tab Guide Code Tab Guide Programming Guide Transition Guide Preferences Guide Version History
Advanced Tools
Extension Manager Shapes Editor BehaviorSpace System Dynamics HubNet HubNet Authoring Logging Controlling Mathematica Link NetLogo 3D NetLogoLab Cluster Computing (HPC)
Extensions
Extensions Guide Arduino Array Bitmap CSV GIS GoGo LevelSpace Matrix Networks Palette Profiler Python Resource Rnd Sound Simple R Table Time Vid View2.5D
FAQ
primitive: sort
NetLogo Dictionary
  • No items found

sort 1.0

sort list

sort agentset

Reports a sorted list of numbers, strings, or agents.

If the input contains no numbers, strings, or agents, the result is the empty list.

If the input contains at least one number, the numbers in the list are sorted in ascending order and a new list reported; non-numbers are ignored.

Or, if the input contains at least one string, the strings in the list are sorted in ascending order and a new list reported; non-strings are ignored.

Or, if the input is an agentset or a list containing at least one agent, a sorted list of agents (never an agentset) is reported; non-agents are ignored. Agents are sorted in the same order the < operator uses. (Patches are sorted with the top left-most patch first and the bottom right-most patch last, turtles are sorted by whonumber).

show sort [3 1 4 2]
=> [1 2 3 4]
show sort [2 1 "a"]
=> [1 2]
show sort (list "a" "c" "b" (patch 0 0))
=> ["a" "b" "c"]
show sort (list (patch 0 0) (patch 0 1) (patch 1 0))
=> [(patch 0 1) (patch 0 0) (patch 1 0)]

;; label patches with numbers in left-to-right, top-to-bottom order
let n 0
foreach sort patches [ the-patch ->
ask the-patch [
set plabel n
set n n + 1
]
]

;; some additional examples to clarify behavior in strange cases
show sort (list patch 0 0 patch 0 1 patch 1 0 turtle 0 turtle 1) ; turtles are always sorted lower than patches
=> [(turtle 0) (turtle 1) (patch 0 1) (patch 0 0) (patch 1 0)]
show sort (list nobody false true) ; booleans and nobody cannot be sorted
=> []
show sort (list [1 2 3] turtles) ; lists and agentsets are not included if they are inside a list passed to sort
=> []

See also sort-by, sort-on.

Take me to the full NetLogo Dictionary.

NetLogo Dictionary: size

Documentation for the size primitive.

NetLogo Dictionary: sort-by

Documentation for the sort-by primitive.


NetLogo is a programmable modeling environment for simulating natural and social phenomena. It was authored by Uri Wilensky in 1999 and has been in continuous development ever since at the Center for Connected Learning and Computer-Based Modeling.

Related Links
  • NetLogo Home
  • CCL Home
  • NetLogo Web
  • NetTango Web
  • NetLogo 3D
  • BehaviorSearch
  • Contact Us

Copyright © 1999-2026 Uri Wilensky and the Center for Connected Learning and Computer-Based Modeling at Northwestern University . All rights reserved.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License , or (at your option) any later version.

Commercial licenses are also available. To inquire about commercial licenses, please contact Uri Wilensky at [email protected] .

For more information, visit the NetLogo website .