有一个班级集合 属性为班级人数classnum和班级名字classname
对其排序,按人数排,人数相同在按班级名字排,请问怎么排序啊?要用什么方法哦 ?

解决方案 »

  1.   

    用冒泡算法
    我在网上搜了一个例子,改写了一下,供你参考
    public class BubbleSort 

      /**        *对数组obj中的元素以冒泡排序算法进行排序        */ 
           public void sort(int[] obj) 
           {     if (obj == null)               {    throw new NullPointerException("The argument can not be null!"); 
                  } 
                  int tmp; 
                  for (int i = 0 ;i < obj.length ;i++ ) 
                  {    //切记,每次都要从第一个开始比。最后的不用再比。 
                         for (int j = 0 ;j < obj.length - i - 1 ;j++ ) 
                         {   //对邻接的元素进行比较,如果后面的小,就交换 
                                if (obj[j]-(obj[j + 1]) > 0) 
                                {  tmp = obj[j]; 
                                       obj[j] = obj[j + 1]; 
                                       obj[j + 1] = tmp; 
                                }   } 
                  }  } 
           public static void main(String[] args){
            BubbleSort bs=new BubbleSort();
            int[] i={21,12,3,34,0,3,8,9};
            bs.sort(i);
            for(int j=0;j<i.length;j++){
            System.out.println(i[j]);
            }
           }
    }
      

  2.   

    去我blog看最新一篇的文章吧 
      

  3.   

    public class Count{
         public void sort(String[] obj) 
          {    if (obj == null)               {    throw new NullPointerException("The argument can not be null!"); 
                  }
                    int[] math=new int[obj.length];
                    String[] str=new String [obj.length];
                    for(int i=0;i<obj.length;i++){
                    int leftbrace=obj[i].indexOf('[');
                    str[i]=obj[i].substring(0,leftbrace);    
                    String port=obj[i].substring(leftbrace+1,obj[i].length()-1); 
                    math[i]=Integer.parseInt(port);              }
                    
                  int tmp; 
                   String s;
                  for (int i = 0 ;i < math.length ;i++ ) 
                  {      
                        for (int j = i+1 ;j < math.length ;j++ ) 
                        {   
                                if (math[i]-math[j] > 0) 
                                {     tmp = math[i]; 
                                      math[i] = math[j]; 
                                      math[j] = tmp;
                                       s = str[i]; 
                                      str[i] = str[j]; 
                                      str[j] = s;
                                                                         
                                }  } 
                  } 
                  for(int i=0;i<math.length;i++){
                   if(math[i]==math[i+1]){
                     if(str[i].compareTo(str[i+1])>0){
                                      tmp = math[i]; 
                                      math[i] = math[i+1]; 
                                      math[i+1] = tmp;
                                      s = str[i]; 
                                      str[i] = str[i+1]; 
                                      str[i+1] =s;
                                   }
                     
                    
                 
                    
                      }  
                   System.out.println(str[i]+math[i]); 
            } 
                  System.out.println(str[math.length-1]+math[math.length-1]);
          }   
          public static void main(String[] args){ 
          try{
          Count bs=new Count(); 
          
          String[] bj={"计算机一班[43]","计算机二班[46]","计算机三班[43]","计算机四班[50]"}; 
          bs.sort(bj); 
          
          }
          catch(ArrayIndexOutOfBoundsException e){
           e.printStackTrace();
            
          }
            
          } 
      

  4.   

    XIEXIExiexi谢谢啊  ,学到了很多 看了justinavril 的BLOG,,不错