home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OL.LZH / PROGS.LZH / ADLSORT.ICN < prev    next >
Text File  |  1991-09-05  |  2KB  |  88 lines

  1. ############################################################################
  2. #
  3. #    Name:    adlsort.icn
  4. #
  5. #    Title:    Sort address list entries
  6. #
  7. #    Author:    Ralph E. Griswold
  8. #
  9. #    Date:    September 2, 1991
  10. #
  11. ############################################################################
  12. #
  13. #    This program sorts entries in address lists.  The options are:
  14. #
  15. #    -c    by country
  16. #    -n    by name
  17. #    -z    by ZIP code
  18. #
  19. #     The default is -n.  If more than one option is specified, the
  20. #    order of dominance is -n -z -c.
  21. #
  22. ############################################################################
  23. #
  24. #  See also: address.doc, adlcount.icn, adlfilter.icn, adllist.icn,
  25. #     adlsort,icn, labels.icn
  26. #
  27. #  Links: adlutils, options, namepfx
  28. #
  29. ############################################################################
  30.  
  31. link adlutils, options, namepfx
  32.  
  33. procedure main(args)
  34.    local item, item_lists, opts, sort_method
  35.  
  36.    item_lists := table()
  37.  
  38.    sort_method := "n"            # The default is sorting by name.
  39.    get_item := get_lastname
  40.  
  41.    opts := options(args,"cnz")
  42.  
  43.    if \opts["c"] then {            # If more than one given, last applies.
  44.       sort_method := "c"
  45.       get_item := get_country
  46.       }
  47.    if \opts["z"] then {
  48.       sort_method := "z"
  49.       get_item := get_zipcode
  50.       }
  51.    if \opts["n"] then {
  52.       sort_method := "n"
  53.       get_item := get_lastname
  54.       }
  55.  
  56.    while add := nextadd() do {
  57.       item := get_item(add)
  58.       /item_lists[item] := []
  59.       put(item_lists[item],add)
  60.       }
  61.        
  62.    item_lists := sort(item_lists,3)
  63.  
  64.    if sort_method == ("c" | "z") then {
  65.       while get(item_lists) do
  66.          every writeadd(!get(item_lists))
  67.       }
  68.  
  69.    else if sort_method == "n" then {
  70.       while get(item_lists) do {
  71.          names := get(item_lists)
  72.          if *names = 1 then writeadd(names[1])    # avoid flap for common case
  73.          else {
  74.             prefixes := table()
  75.             every add := !names do {
  76.                prefix := namepfx(add.text)
  77.                /prefixes[prefix] := []
  78.                put(prefixes[prefix],add)
  79.                }
  80.             prefixes := sort(prefixes,3)
  81.             while get(prefixes) do
  82.                every writeadd(!get(prefixes))
  83.             }
  84.          }
  85.       }
  86.  
  87. end
  88.