* SORT
* !SORT() subroutine
* Copyright (c) 1995, Ladybridge Systems, All Rights Reserved
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 
* Ladybridge Systems can be contacted via the www.openqm.com web site.
* 
* START-HISTORY:
* 16 Sep 04  2.0-1 OpenQM launch. Earlier history details suppressed.
* END-HISTORY
*
* START-DESCRIPTION:
*
* CALL !SORT(in.list, out.list, mode)
*
* in.list  = mark delimited list of items to sort. Any marks may be used.
* out.list = field mark delimited sorted list
* mode     = Sort mode : A = ascending
*                        D = descending
*                        R = right aligned
*                        N = omit null entries
*                        U = unique - remove duplicates
*            Unrecognised or conflicting modes are ignored
*
* END-DESCRIPTION
*
* START-CODE

$internal
$no.symbols
$no.xref

subroutine !sort(in.list, out.list, mode)
$catalog !sort

$include int$keys.h

   if len(in.list) = 0 then
      out.list = in.list
      return
   end

   u.mode = upcase(mode)

   dim sort.keys(1)
   sort.keys(1) = 0
   if index(u.mode, "D", 1) then sort.keys(1) += BT.DESCENDING
   if index(u.mode, "R", 1) then sort.keys(1) += BT.RIGHT.ALIGNED
   if index(u.mode, "U", 1) then sort.keys(1) += BT.UNIQUE
   sort.tree = btree(1, sort.keys)

   no.nulls = index(u.mode, "N", 1)

   loop
      s = remove(in.list, delim)
      if len(s) or not(no.nulls) then add s to sort.tree
   while delim
   repeat

   out.list = sort.tree : ""
   return
end

* END-CODE
