/* Array or list sort and reverse */

# Sort

#<- ArraySort, ListSort

SORT_* :
 ARRAY
 LIST
;

*_SORT :
 QUICK
 SHUFFLE
 REVERSE
 NO
;

/* * */

Sort ->
Arg Array : ^&Array

- ArraySort 'Array' Sort_method
' Sort_method = SortMethod( Already_ordered, Array._, SORT_ARRAY )
' Already_ordered = AlreadyOrdered( Array )

/

AlreadyOrdered Sort ->
Arg Array : Array,
  - Counter = 0 ..

Do Counter ++ [ Array.i < Array.( i + 1 ) ]
Where i = 1 to Array._ - 1

/

Sort ->
Arg Dest : &List : T, Src : List : <T>

New : List : [Src] = <> ..

Dest = New
' - ListSort >New< Src Sort_method
' Sort_method = SortMethod( Already_ordered, Src._, SORT_LIST )
' Already_ordered = AlreadyOrdered( Src )

/

AlreadyOrdered Sort ->
Arg List : List,
  - Counter = 0 ..

Prev = . ..

Do get Element from List
 Z = Prev = Element
 Counter ++ [ Z && Z < Element ]
;

/

/* Action selectors */

SortMethod Sort ->
Arg Ordered, Total : Integer, 
    . : SORT_ARRAY

NO_SORT      << Total = 0 | Total = 1
NO_SORT      << Ordered = Total - 1
REVERSE_SORT << Ordered = 0

             >> SHUFFLE_SORT

/

SortMethod Sort ->
Arg Ordered, Total : Integer, 
    . : SORT_LIST

NO_SORT      << Total = 0 | Total = 1
NO_SORT      << Ordered = Total - 1
REVERSE_SORT << Ordered = 0

             >> QUICK_SORT

/

/* Empty functions */

ArraySort '?', NO_SORT = _
ListSort >?<, ?, NO_SORT = _

/* Exports */

# >> Sort