
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


Sort an array of composite structures by a key.



For example, if you define a composite structure that presents a name-value pair (in pseudo-code):

Define structure pair such that: 
   name as a string
   value as a string

and an array of such pairs:

   x: array of pairs

then define a sort routine that sorts the array x by the key name.

This task can always be accomplished with Sorting Using a Custom Comparator.

If your language is not listed here, please see the other article.




 
Module CheckIt {
      Flush ' empty stack of values
      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()
      Quick.LE=lambda (a, b)->{
            =a.name$<=b.name$
      }
      Data "Joe",   5531
      Data "Adam", 2341
      Data "Bernie", 122
      Data "Walter", 1234
      Data "David",  19
      Class pair {
            name$
            value_
      }
      Document Doc$={Unsorted Pairs:
      }
      Dim A(1 to 5)=pair()
      For i=1 to 5 {
            For A(i) {
                  Read .name$, .value_
                  Doc$=Format$("{0}, {1}", .name$, .value_)+{
                  }
            }
      }
 
      Call Quick.quicksort(&A(),1, 5)
      Doc$={
            Sorted Pairs
            }
      k=Each(A())
      While k {
            getone=array(k)
            For getone {
                              Doc$=Format$("{0}, {1}", .name$, .value_)+{
                              }
            }
      }
      Report Doc$
      Clipboard Doc$
}
Checkit
module Checkit2 {
      Inventory Alfa="Joe":=5531, "Adam":=2341, "Bernie":=122
      Append Alfa, "Walter":=1234, "David":=19
      Sort Alfa
      k=Each(Alfa)
      While k {
            Print eval$(Alfa, k^), Eval(k)
      }
}
Checkit2
module Checkit3 {
      class any {
             x
            class:
            Module any (.x) {}
      }
      Inventory Alfa="Joe":=any(5531), "Adam":=any(2341), "Bernie":=any(122)
      Append Alfa, "Walter":=any(1234), "David":=any(19)
      Sort Alfa
      k=Each(Alfa)
      While k {
            \\ k^ is the index number by k cursor
            \\ Alfa("joe") return object
            \\ Alfa(0!) return first element object
            \\ Alfa(k^!) return (k^) objext
            Print eval$(Alfa, k^),  Alfa(k^!).x
      }
}
Checkit3
 