list of "who" numbers in order of a turtles-own variable?

19 views
Skip to first unread message

Unruh Lee

unread,
Jun 20, 2022, 6:54:45 PM6/20/22
to netlogo-users
Hi and thanks in advance for any help, 

In a model like Wealth Distribution, in the built-in models library of Netlogo, you can create an ordered list of turtles' "wealth" variable, thus: 

    let sorted-wealths sort [wealth] of turtles

How would one create a corresponding list of the "who" numbers of all those turtles? That is, in order of their "wealth" variable? 

This seems like it should be simple but I'm not having luck figuring it out. 

Thanks, 
Lee

Wade Schuette

unread,
Jun 20, 2022, 7:51:58 PM6/20/22
to Unruh Lee, netlogo-users
You need "sort-on"

;; =============== try this -==============
turtles-own [ wealth ]
to setup
  clear-all
  create-turtles 8 [ set wealth random 100 ]

  let sorted-wealths sort [wealth] of turtles
  print sorted-wealths
  let sorted-turtles sort-on [wealth ]  turtles
  print sorted-turtles
  foreach sorted-turtles [ x -> print (word [who] of x " has $" [wealth] of x)]
end
;;================================================
 

--
You received this message because you are subscribed to the Google Groups "netlogo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to netlogo-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/netlogo-users/19107562-2a42-45cd-a01e-5d500a10c467n%40googlegroups.com.

Unruh Lee

unread,
Jun 20, 2022, 9:05:54 PM6/20/22
to Wade Schuette, netlogo-users
You just made me realize I was using an old version of the Netlogo primitives dictionary, which did not include "sort-on"! 

Thank you, Wade!
Lee

James Steiner

unread,
Jun 21, 2022, 9:28:57 AM6/21/22
to Unruh Lee, Wade Schuette, netlogo-users
Also: note that Wade showed you how sort-on gives you a list turtles, not who numbers, which is a best practice.    Unless you have a very good reason to do so, never work with who numbers ,,, just work with the turtles. 

Unruh Lee

unread,
Jun 28, 2022, 6:28:59 PM6/28/22
to James Steiner, Wade Schuette, netlogo-users
Thank you, James!
Lee
Reply all
Reply to author
Forward
0 new messages