Task

Define a function/method/subroutine which sorts a sequence ("table") of sequences ("rows") of strings ("cells"), by one of the strings. Besides the input to be sorted, it shall have the following optional parameters:





 ordering
 A function specifying the ordering of strings; lexicographic by default.
 column
 An integer specifying which string of each row to compare; the first by default.
 reverse
 Reverses the ordering.


This task should be considered to include both positional and named optional parameters, as well as overloading on argument count as in Java or selector name as in Smalltalk, or, in the extreme, using different function names. Provide these variations of sorting in whatever way is most natural to your language. If the language supports both methods naturally, you are encouraged to describe both.

Do not implement a sorting algorithm; this task is about the interface. If you can't use a built-in sort routine, just omit the implementation (with a comment).

See also:

 Named Arguments


declare
  class Table
     attr
        rows
 
     meth init(Rows)
        rows := Rows
     end
 
     meth sort(ordering:O<=Lexicographic  column:C<=1  reverse:R<=false)
        fun {Predicate Row1 Row2}
           Res = {O {Nth Row1 C} {Nth Row2 C}}
        in
           if R then {Not Res} else Res end
        end
     in
        rows := {Sort @rows Predicate}
     end
  end
 
  fun {Lexicographic As Bs}  %% omitted for brevity
  end
 
  T = {New Table init([["a" "b" "c"] ["" "q" "z"] ["zap" "zip" "Zot"]])}
in
  {T sort}
  {T sort(column:3)}
  {T sort(column:2)}
  {T sort(column:2 reverse:true)}
  {T sort(ordering:fun {$ A B} {Length B} < {Length A} end)}