gsort.hlp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. General List Sorting Utilities MLG - 22 December 1981
  2. ------------------------------
  3. The module Gsort (use LOAD GSORT) contains a number of general sorting
  4. functions and associated key comparison functions. The Key comparison
  5. functions are given 2 objects to compare, return NIL if they are not
  6. in correct order:
  7. BeforeFn(a:any,b:any):Extra-Boolean; % return NIL if not in order
  8. The package defines:
  9. NumberSortFn(N1:number,N2:Number)
  10. StringSortFn(S1:String,N2:string) [Sc1 and Sc2 are faster versions]
  11. IdSortFn(D1:id,D2:id) [IdC1 and IDc2 are faster]
  12. AtomSortFn(X1:atom,X2:Atom)
  13. The general sorting functions expect a SortFn (which MUST be an ID)
  14. GsortP(Lst:x-list,BeforeFn:id):Boolean % T if x-list is sorted
  15. Gsort(Lst:x-list,BeforeFn:id):x-list % Tree-sort of x-list
  16. GMergeSort(Lst:x-list,BeforeFn:id):x-list % Merge-sort of x-list
  17. Currently, Gsort is often fastest, but GMergeSort is more stable.
  18. Example: To sort a list of Ids call Gsort(Dlist,'Idsortfn)
  19. or Gsort(Dlist,'IDc2) for faster sort.
  20. To sort list of records (e.g. pairs), user must define comparison:
  21. E.g. to sort LP, a List of dotted pairs (Number . Info), define
  22. procedure NPSortFn(P1,P2); NumberSortFn(Car p1, Car P2);
  23. then execute Gsort(LP,'NPSortfn);
  24. See PU:Gsort.Red for the code.