[Biojava-l] Sort symbols in an alphabet

Schreiber, Mark mark.schreiber@agresearch.co.nz
Fri, 10 Jan 2003 09:39:05 +1300


The following code should do it,

You could actually make the ComparableSymbol wrapper implement Symbol
and pass all the Symbol methods directly to the wrapped Symbol. I may
even submit a ComparableSymbol as a class to biojava-live if people
think they would use it.

- Mark


import org.biojava.bio.seq.*;
import org.biojava.bio.symbol.*;
import java.util.*;

public class SortAlphabetByName {
  public static void main(String[] args) {
    List l = new ArrayList();

    //add Symbols out of order
    l.add(new ComparableSymbol(DNATools.c()));
    l.add(new ComparableSymbol(DNATools.g()));
    l.add(new ComparableSymbol(DNATools.t()));
    l.add(new ComparableSymbol(DNATools.a()));

    //sort them
    Collections.sort(l);

    //print there names
    for (int i = 0; i < l.size(); i++) {
      ComparableSymbol cs = (ComparableSymbol)l.get(i);
      System.out.println(cs.getWrapped().getName());
    }

  }

  // a Mix in wrapper
  static class ComparableSymbol implements Comparable{
    private Symbol wrapped;

    public ComparableSymbol(Symbol wrapped){
      this.wrapped = wrapped;
    }

    public int compareTo(Object o){
      Symbol s = ((ComparableSymbol)o).getWrapped();

      return wrapped.getName().compareTo(s.getName());
    }

    public Symbol getWrapped(){
      return wrapped;
    }
  }
}
> -----Original Message-----
> From: Ren, Zhen [mailto:zren@amylin.com] 
> Sent: Friday, 10 January 2003 8:52 a.m.
> To: biojava-l@biojava.org
> Subject: [Biojava-l] Sort symbols in an alphabet
> 
> 
> Hi,
> 
> Symbols in an alphabet is not sorted in any way.  How do I 
> sort them, for instance, by name?  Thanks.
> 
> Zhen
> 
> _______________________________________________
> Biojava-l mailing list  -  Biojava-l@biojava.org 
> http://biojava.org/mailman/listinfo/biojava-l
> 
=======================================================================
Attention: The information contained in this message and/or attachments
from AgResearch Limited is intended only for the persons or entities
to which it is addressed and may contain confidential and/or privileged
material. Any review, retransmission, dissemination or other use of, or
taking of any action in reliance upon, this information by persons or
entities other than the intended recipients is prohibited by AgResearch
Limited. If you have received this message in error, please notify the
sender immediately.
=======================================================================