SortByNonDestructive
  globals [glob1 glob2]
  O> set glob1 [5 4 3 2 1]
  O> set glob2 sort-by [?1 < ?2] glob1
  glob1 => [5 4 3 2 1]
  glob2 => [1 2 3 4 5]

SortByDoesntExposeAgentsetOrder
  O> crt 3
  length remove-duplicates n-values 200 [sort-by [false] turtles] => 6

SortingListsOfAgents
  O> crt 5
  map [[who] of ?] sort (list (turtle 4) (turtle 2) (turtle 0) (turtle 3) (turtle 1)) => [0 1 2 3 4]
  map [list [pxcor] of ? [pycor] of ?] sort (list (patch 1 1) (patch -1 1) (patch 1 -1) (patch -1 -1)) => [[-1 1] [1 1] [-1 -1] [1 -1]]

SortingListsOfLinks
  directed-link-breed [directed-links directed-link]
  undirected-link-breed [undirected-links undirected-link]
  O> crt 5
  T> create-links-with turtles with [ who > [who] of myself ]
  map [[(list [who] of end1 [who] of end2) ] of ?] sort (list (link 1 4) (link 2 3) (link 1 2) (link 0 3) (link 1 3)) => [[0 3] [1 2] [1 3] [1 4] [2 3]]
  O> ask links [ die ]
  T> create-directed-links-to turtles with [ who > [who] of myself ]
  map [[(list [who] of end1 [who] of end2) ] of ?] sort (list (directed-link 2 3) (directed-link 0 3) (directed-link 1 3)) => [[0 3] [1 3] [2 3]]
  T> create-undirected-links-with turtles with [ who > [who] of myself ]
  map [[(list [who] of end1 [who] of end2) ] of ?] sort (list (undirected-link 1 4) (directed-link 1 4) (undirected-link 1 2) (directed-link 0 3) (undirected-link 1 3)) => [[0 3] [1 2] [1 3] [1 4] [1 4]]
  map [ (word [breed] of ? )] sort (list (undirected-link 1 4) (directed-link 1 4) (undirected-link 1 2) (directed-link 0 3) (undirected-link 1 3)) => ["directed-links" "undirected-links" "undirected-links" "directed-links" "undirected-links"]

SortingAgentsets
  O> crt 5
  sort turtles = sort-by [[who] of ?1 < [who] of ?2] turtles => true
  reverse sort turtles = sort-by [[who] of ?1 > [who] of ?2] turtles => true

SortingLinkAgentsets
  O> crt 5 [ create-links-with turtles with [ who > [who] of myself  ] ]
  sort links = sort-by [ [ [who] of end1 * 100 + [who] of end2 ] of ?1  < [ [who] of end1 * 100 + [who] of end2 ] of ?2 ] links => true
  reverse sort links = sort-by [ [ [who] of end1 * 100 + [who] of end2 ] of ?1  > [ [who] of end1 * 100 + [who] of end2 ] of ?2 ] links => true

SortingTypeErrors
  O> crt 10 [ create-links-with other turtles ]
  sort item 0 sort links => ERROR SORT expected input to be a list or agentset but got the link (link 0 1) instead.
  sort-by [[color] of ?] item 0 sort links => ERROR SORT-BY expected input to be a list or agentset but got the link (link 0 1) instead.

sort-on-1
  O> crt 5
  map [[who] of ?] sort-on [who] turtles => [0 1 2 3 4]

sort-on-2
  O> crt 5
  map [[who] of ?] sort-on [(- who)] turtles => [4 3 2 1 0]

sort-on-3
  O> crt 5
  map [[who] of ?] sort-on [(word (5 - who))] turtles => [4 3 2 1 0]

sort-on-4
  O> crt 5
  map [[who] of ?] sort-on [self] turtles => [0 1 2 3 4]

sort-on-doesnt-expose-agentset-order
  O> crt 3
  length remove-duplicates n-values 200 [sort-on [0] turtles] => 6

sort-on-error-handling
  sort-on [__boom] patches => ERROR boom!

# need to set random seed here otherwise sometimes we get "string and a number", sometimes "number and a string"
sort-on-rejects-mixed-types
  O> random-seed 0
  O> crt 3 [ set label who ]
  O> ask turtle 2 [ set label "foo" ]
  sort-on [label] turtles => ERROR SORT-ON works on numbers, strings, or agents of the same type, but not on a string and a number

sort-on-respects-with-local-randomness
  globals [glob1 glob2]
  O> random-seed 0
  O> crt 10
  O> with-local-randomness [ set glob1 map [[who] of ?] sort-on [xcor] turtles ]
  O> with-local-randomness [ set glob2 map [[who] of ?] sort-on [xcor] turtles ]
  glob1 = glob2 => true

sort-by-catches-java-7-general-contract-violation-error
  globals [err]
  to test __ignore sort-by [?1 <= ?2] n-values 1000 [random 5] end
  O> carefully [ test ] [ set err error-message ]
  err => "predicate is not a strictly-less-than or strictly-greater than relation"
