你的node是什么对象啊,不过可以如下
for (int i=4;i>0;i--){
node.getXXX(i)}

解决方案 »

  1.   

    将Node对象全部放进一个数组
    Node[] nodeArray = new Node[4];
    ...
    Arrays.sort(nodeArray,new Comparator(){
     public int compare(Object o1, Object o2) {
       n1 = (Node)o1;
       n2 = (Node)o2;
       return n1.name.compareTo(n2.name);//你自己写,这里假设名字属性为name
     }
               
    });
      

  2.   

    我自己写了一个排序,行是行了,但感觉太麻烦了,有没有简练些的??
    import java.util.*;
    public class TestJava {  public TestJava() {
         ArrayList list = new ArrayList();
         Node node = new Node("1","zsdf");
         list.add(node);
         node = new Node("4","bsdf");
         list.add(node);
         node = new Node("3","ksdf");
         list.add(node);
         sort(list);
         for (int i=0;i<list.size();i++){
           ((Node)list.get(i)).toStrings();
    //       System.out.println();
         }
      }
      void sort(ArrayList list){
         //ArrayList list1 = new ArrayList();
         for (int i=0;i<list.size()-1;i++){
           for (int j=i+1;j<list.size();j++){
             String str1 = ((Node)list.get(i)).get_Name();
             String str2 = ((Node)list.get(j)).get_Name();
             if (str1.compareTo(str2)>0){
                Node node = ((Node)list.get(j));
                list.set(j,((Node)list.get(i)));
                list.set(i,node);
             }
           }
         }
      }
    //  private boolean flag;
      public static void main(String[] args){
         new TestJava();
      }
      class Node{
        private String id;
        private String name;
        public Node(String id,String name){
          this.id = id;
          this.name = name;
        }
        public String get_Name(){
            return name;
        }
        public void toStrings(){
            System.out.println("" +id + ": " + name);
        }
      }
    }
      

  3.   

    Obviously, all you need is just a black-red tree. In Java, try:---------------------------------------------
    // In java.util package
    public class TreeSet
    extends AbstractSetimplements SortedSet, Cloneable, Serializable
    This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.This implementation provides guaranteed log(n) time cost for the basic operations (add, remove and contains).Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all key comparisons using its compareTo (or compare) method, so two keys that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
      

  4.   

    black-red tree  :means what??
    TreeMap的主要应用??我看过TreeMap的例子,TreeMap的排序算法是什么??
    我感觉一列数经过TreeMap后好像是随机输出的??
      

  5.   

    很简单,利用Collections.sort方法,如下是我的程序段:
    data是一个Vector,它的元素也是Vector.这个排序方法
    是对data内部,根据data的元素的元素进行排序Collections.sort(data,new Comparator()
    {    public int compare(Object a,Object b)
       {
         String str1 = (String)((Vector)a).elementAt(i);      String str2 = (String)((Vector)b).elementAt(i);
         return str1.compareTo(str2);    
                 } }
     );
    return data;