
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.




/*REXX program  sorts an array of composite structures  (which has two classes of data).*/
#=0                                              /*number elements in structure (so far)*/
name='tan'   ;  value= 0;  call add name,value   /*tan    peanut M&M's are  0%  of total*/
name='orange';  value=10;  call add name,value   /*orange    "    "     "  10%   "   "  */
name='yellow';  value=20;  call add name,value   /*yellow    "    "     "  20%   "   "  */
name='green' ;  value=20;  call add name,value   /*green     "    "     "  20%   "   "  */
name='red'   ;  value=20;  call add name,value   /*red       "    "     "  20%   "   "  */
name='brown' ;  value=30;  call add name,value   /*brown     "    "     "  30%   "   "  */
call show  'before sort',  #
say  copies('▒', 70)
call xSort                 #
call show  ' after sort',  #
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
add:   procedure expose # @.;   #=#+1            /*bump the number of structure entries.*/
       @.#.color=arg(1);      @.#.pc=arg(2)      /*construct a entry of the structure.  */
       return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show:  procedure expose @.;   do j=1  for arg(2) /*2nd arg≡number of structure elements.*/
                              say right(arg(1),30)  right(@.j.color,9)  right(@.j.pc,4)'%'
                              end   /*j*/        /* [↑]  display  what,  name,  value.  */
       return
/*──────────────────────────────────────────────────────────────────────────────────────*/
xSort: procedure expose @.; parse arg N;    h=N
                              do while h>1;                       h=h%2
                                do i=1  for N-h;        j=i;      k=h+i
                                  do  while @.k.color<@.j.color         /*swap elements.*/
                                  _=@.j.color;        @.j.color=@.k.color;     @.k.color=_
                                  _=@.j.pc;           @.j.pc   =@.k.pc;        @.k.pc   =_
                                  if h>=j  then leave;    j=j-h;    k=k-h
                                  end   /*while @.k.color ···*/
                                end     /*i*/
                              end       /*while h>1*/
       return