[Biojava-l] Sort symbols in an alphabet

Thomas Down td2@sanger.ac.uk
Fri, 10 Jan 2003 11:58:42 +0000


On Thu, Jan 09, 2003 at 11:52:01AM -0800, Ren, Zhen wrote:
> Hi,
> 
> Symbols in an alphabet is not sorted in any way. 
> How do I sort them, for instance, by name?  Thanks.

Alphabets are, by design, pretty straigforward sets of symbol
objects.  If you want to print them out in some specific order,
how about:

   FiniteAlphabet fa = ...
   List faSymbols = new ArrayList();
   for (Iterator i = fa.iterator(); i.hasNext(); ) {
       faSymbols.add(i.next());
   }
   Collections.sort(
       faSymbols,
       new Comparator() {
           public int compare(Object o1, Object o2) {
               Symbol s1 = (Symbol) o1;
               Symbol s2 = (Symbol) o2;
               return s1.getName().compareTo(s2.getName());
           }
       }
   );


Thomas.