
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other sorting algorithms,   see sorting algorithms,   or:

O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort


Task

Sort an array (or list) of strings in order of descending length, and in ascending lexicographic order for strings of equal length.

Use a sorting facility provided by the language/library, combined with your own callback comparison function.


Note:   Lexicographic order is case-insensitive.




 
Module Checkit {
      Class Quick {
            Private:
         partition=lambda-> {
               Read &A(), p, r : i = p-1 : x=A(r)
               For j=p to r-1 {If .LE(A(j), x) Then i++:Swap A(i),A(j)
               } : Swap A(i+1), A(r) :  Push  i+2, i 
         }
            Public:
         LE=Lambda->Number<=Number
         Module ForStrings {
               .partition<=lambda-> {
                     Read &a$(), p, r : i = p-1 : x$=a$(r)
                     For j=p to r-1 {If a$(j)<= x$ Then i++:Swap a$(i),a$(j)
                     } : Swap a$(i+1), a$(r) : Push i+2, i
               }
         }
         Function quicksort {
              Read ref$
              {
                      loop : If Stackitem() >= Stackitem(2) Then Drop 2 : if  empty then {Break} else continue
                      over 2,2 : call .partition(ref$) :shift 3 
              }
         }
      }
      Quick=Quick()
 
      ToSort$="this is a set of strings to sort This Is A Set Of Strings To Sort"
      Dim a$()
      a$()=Piece$(ToSort$, " ")
      \\ we can redim to any range
      Dim a$(100 to len(a$())+99)  ' from 100 to 115 (16 items)
      Group Quick {
            Module ForStringsSpecial {
               .partition<=lambda-> {
                        Read &a$(), p, r : i = p-1 : x$=a$(r) :lx$=lcase$(x$) : k=len(x$)
                        For j=p to r-1 {
                              m=len(a$(j))
                              select case compare(m, k)
                              case 0
                              {
                                   aj$=lcase$(a$(j))                              
                                   if aj$>lx$ then exit
                                   if aj$=lx$ then if a$(j)<=x$ then exit
                                   i++ 
                                   Swap a$(i),a$(j) 
                              }
                              case 1
                              {
                                    i++:Swap a$(i),a$(j)
                              }
                              End Select
                     } : Swap a$(i+1), a$(r) : Push i+2, i
               }
         }
      }
      Document doc$={Unsorted List:
      } 
      k=each(a$())
      While k {
            doc$="   "+array$(k)+{
            }
      }
      Quick.ForStringsSpecial
      \\ Dimension(a$(), 0, 1) is Lbound a$() first dimension
      \\ Dimension(a$(), 0, 1) is Ubound a$() first dimension
      Call Quick.quicksort(&a$(), Dimension(a$(), 0, 1), Dimension(a$(), 1,1))
      k=each(a$())
      Doc$={
            Sorted List:
            }
      While k {
            doc$="   "+array$(k)+{
            }
      }
      Report doc$
      Clipboard doc$
}
Checkit
 