home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / ipl / progs / adlsort.icn < prev    next >
Text File  |  2000-07-29  |  2KB  |  93 lines

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