Joan
Joan

Reputation: 135

Netlogo sort variables

I want to order these numbers in variables.
I tested with Ifs and didn't found the way of how to do it.

Then I tested with sort but it seems that i doesn't accept variables.

let ginyo00 [xcor] of coche 0     
let ginyo01 [xcor] of coche 1
let ginyo02 [xcor] of coche 2
let ginyo03 [xcor] of coche 3
output-print ginyo00
output-print ginyo01
output-print ginyo02
output-print ginyo03
output-print "====="
set maxv max (list ginyo00 ginyo01 ginyo02 ginyo03)
show sort [ginyo00 ginyo01 ginyo02 ginyo03]

Error that shows

I need a way of sorting this values and knowing which are.

Upvotes: 0

Views: 107

Answers (1)

Jasper
Jasper

Reputation: 2780

The sort primitive works on lists or agent sets, per the docs.

So you can use it with variables the same way you got the max, by using the list primitive to turn your variables into a list for sort:

show sort (list ginyo00 ginyo01 ginyo02 ginyo03)

You can also get this info without the intermediate variables:

let my-coches coches with [ who < 4 ]
ask my-coches [ output-print xcor ]
set maxv max ([ xcor ] of my-coches)
show sort ([ xcor ] of my-coches)

If what you actually want is the coches in sorted order instead of the values in order, you can use sort-on instead:

let sorted-coches sort-on [xcor] my-coches
show sorted-coches

Upvotes: 2

Related Questions