2

I am looking for a solution in Netlogo to get a value from a list of lists with the minimum pair value.

((value1,value2)(value1,value2)...)

As an example I have a list:

ListofLists = ((2,3)(5,8)(1,9))

I want to select the list with the minimum first value. --> here (1,9) and write the second value out of it.

Goal: Get 9!

my try looks like this:

ListofLists = ((2,3)(5,8)(1,9))
    let ChoosedList []
    set ChoosedList min (item 0 ListofLists)
    set ChoosedValue item 1 ChoosedList

Do you have a solution for this?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
TEck
  • 21
  • 1

1 Answers1

1

The easiest way to do this is to sort the list of lists by the first element of each sublist, then take the second element of the first sublist in the sorted list of lists. Here it is in stages:

let sorted-list sort-by [[a b] -> item 0 a < item 0 b] [[2 3] [5 8] [1 9]]
let ChoosedList first sorted-list
let ChoosedValue item 1 of ChoosedList

or, in one line,

let ChoosedValue item 1 first sort-by [[a b] -> item 0 a < item 0 b] [[2 3] [5 8] [1 9]]

sort-by is very useful for sorting lists of lists.

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Charles
  • 3,888
  • 11
  • 13