Sorting a list of lists by a specific index of internal lists

I have a list that looks something like this:

[["Local 7" 1 "say" "Say: Inspect Fences"] ["Local 7" 1 "do" "Do: Shepherd Cows"] ["Local 6" 1 "say" "Say: Shepherd Cows"] ["Local 6" 1 "do" "Do: Shepherd Cows"] ["Local 6" 2 "say" "Say: Shepherd Cows"] ["Local 6" 2 "do" "Do: Shepherd Cows"] ["Local 7" 2 "say" "Say: Inspect Fences"] ["Local 7" 2 "do" "Do: Shepherd Cows"] ["Local 6" 3 "say" "Say: Shepherd Cows"] ["Local 6" 3 "do" "Do: Shepherd Cows"] ["Local 7" 3 "say" "Say: Inspect Fences"] ["Local 7" 3 "do" "Do: Inspect Fences"]]

      

I would like to sort the list item 1

. (I know this is already in the copy / paste version, but it may not always be.)

sort

just returns an empty list (I'm not even sure why, but I'm guessing it's a separate question) and sort-by

doesn't seem to work because it needs a reporter that resolves the boolean.

Is there a sane way to do this? Or do I need to first get a list of values ​​that I want to sort, then sort, and then loop over that list, creating a new list of values ​​in the original list that matches the corresponding item value?

+3


source to share


1 answer


Edit: Updated with NetLogo 6 syntax

You can easily convert this to sort-by

:

to-report sort-with [ key lst ]
  report sort-by [ [a b] -> (runresult key a) < (runresult key b) ] lst
end

      



Then you use it like this:

sort-with [ l -> item 1 l ] my-list

      

+3


source







All Articles