是这样的,我有一组数据,只有两列,我把每一行都封装到一个对象里面,我想按右边那一列来排序,怎么实现,数据如下:
29 13440896
791 13745707
791 13745707
791 13745707
791 13745707
791 13745707
791 13745707
791 13745707
791 13745707
591 13743596
591 13743596
591 13743596
591 13743596
591 13743596
591 13743596
591 13743596
591 13743596
591 13743597
591 13743597
591 13743597
591 13743597
591 13743597
591 13743597
591 13743597
591 13743597
591 13743599
591 13743599
591 13743599
591 13743599
591 13743599
591 13743599
591 13743599
591 13743599
531 13745530
531 13745530
531 13745530
531 13745530
531 13745530
531 13745530
531 13745530
531 13745530
351 13749355
351 13749355
351 13749355
351 13749355
351 13749355
351 13749355
351 13749355
351 13749355
898 13741766
898 13741766
898 13741766
898 13741766
898 13741766
898 13741766
898 13741766
898 13741766
29 13440892
29 13440892
29 13440892
29 13440892
29 13440892
29 13440892
29 13440892
29 13440892
931 13742930
931 13742930
931 13742930
931 13742930
931 13742930
931 13742930
931 13742930
931 13742930
591 13900690
591 13900690
591 13900690
591 13900690
591 13900690
591 13900690
591 13900690
591 13900690
731 13746742
731 13746742
731 13746742
731 13746742
731 13746742
731 13746742
731 13746742
731 13746742
731 13746841
731 13746841
731 13746841
731 13746841
731 13746841
731 13746841
731 13746841
731 13746841
571 13742575
571 13742575
571 13742575
571 13742575
571 13742575
571 13742575
571 13742575
571 13742575
771 13748781
771 13748781
771 13748781
771 13748781
771 13748781
771 13748781
771 13748781
771 13748781
371 13747388
371 13747388
371 13747388
371 13747388
371 13747388
371 13747388
371 13747388
371 13747388
371 13747381
371 13747381
371 13747381
371 13747381
371 13747381
371 13747381
371 13747381
371 13747381
371 13747383
371 13747383
371 13747383
371 13747383
371 13747383
371 13747383
371 13747383
371 13747383
371 13747384
371 13747384
371 13747384
371 13747384
371 13747384
371 13747384
371 13747384
371 13747384
371 13747385
371 13747385
371 13747385
371 13747385
371 13747385
371 13747385
371 13747385
371 13747385
371 13747387
371 13747387
371 13747387
371 13747387
371 13747387
371 13747387
371 13747387
371 13747387
371 13747380
371 13747380
371 13747380
371 13747380
371 13747380
371 13747380
371 13747380
371 13747380
371 13747382
371 13747382
371 13747382
371 13747382
371 13747382
371 13747382
371 13747382
371 13747382
371 13747386
371 13747386
371 13747386
371 13747386
371 13747386
371 13747386
371 13747386
371 13747386
22 13742214
22 13742214
22 13742214
22 13742214
22 13742214
22 13742214
22 13742214
22 13742214
532 13746530
532 13746530
532 13746530
532 13746530
532 13746530
532 13746530
532 13746530
532 13746530
551 13743960
551 13743960
551 13743960
551 13743960
551 13743960
551 13743960
551 13743960
551 13743960
551 13742960
551 13742960
551 13742960
551 13742960
551 13742960
551 13742960
551 13742960
551 13742960

解决方案 »

  1.   

    自己去继承一个TreeSet,定义好排序的规则。然后把你要排序的对象扔进去。。
      

  2.   


    public keyValue implements Comparable{
       int key;
       int value;   public int compareTo(keyValue  o){
             return this.value<o.value;
       }
    }
    把所有的数组元素,各new一个keyValue 对象, 把所有keyValue装到一个List 然后用Collections.sort(theList) 进行排序。 或是按楼上说的,把所有的keyvalue扔到一个TreeSet,它们会自动排序。
      

  3.   

    [Quote=引用 4 楼 mybeautiful 的回复:]
    Java codepublic keyValue implements Comparable{
       int key;
       int value;   public int compareTo(keyValue  o){
             return this.value<o.value;
       }
    }能否帮我写个完整的代码,我不是很明白,谢谢
      

  4.   

    定义一个对象 实现comparable 接口
    将数据放入list
    Collections.sort(list);即可import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;public class Test7 implements Comparable { private int col1;
    private int col2;


    public int getCol1() {
    return col1;
    } public void setCol1(int col1) {
    this.col1 = col1;
    } public int getCol2() {
    return col2;
    } public void setCol2(int col2) {
    this.col2 = col2;
    } public Test7() {
    // TODO Auto-generated constructor stub
    } @Override
    public int compareTo(Object o) {
    // TODO Auto-generated method stub
    if(o!=null && o.getClass().equals(Test7.class))
    {
    if(this.getCol2()>((Test7)o).getCol2())
    {
    return 1;
    }else if(this.getCol2()<((Test7)o).getCol2())
    {
    return -1;
    }else
    {
    return 0;
    }
    }
    return 0;
    } /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Test7 t1 = new Test7();
    List list = new ArrayList();
    t1.setCol1(371);
    t1.setCol2(13747383);
    list.add(t1);
    Test7 t2 = new Test7();
    t2.setCol1(372);
    t2.setCol2(13747381);
    list.add(t2);
    Test7 t3 = new Test7();
    t3.setCol1(370);
    t3.setCol2(13747399);
    list.add(t3);

    Collections.sort(list);


    for(int i=0;i<list.size();i++)
    {
    System.out.println(((Test7)list.get(i)).getCol2());
    } }}
      

  5.   

    楼上写的很好,不过有一点可以改进的,就是用泛型后,compareTo方法不需要转型了。可以写成。import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;public class Test7 implements Comparable<Test7> {    private int col1;
        private int col2;
        
        
        public int getCol1() {
            return col1;
        }    public void setCol1(int col1) {
            this.col1 = col1;
        }    public int getCol2() {
            return col2;
        }    public void setCol2(int col2) {
            this.col2 = col2;
        }    public Test7() {
            // TODO Auto-generated constructor stub
        }    @Override
        public int compareTo(Test7 o) {
            // TODO Auto-generated method stub
                if(this.getCol2()>o.getCol2())
                {
                    return 1;
                }else if(this.getCol2()<o.getCol2())
                {
                    return -1;
                }else
                {
                    return 0;
                }
        }    /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Test7 t1 = new Test7();
            List list = new ArrayList();
            t1.setCol1(371);
            t1.setCol2(13747383);
            list.add(t1);
            Test7 t2 = new Test7();
            t2.setCol1(372);
            t2.setCol2(13747381);
            list.add(t2);
            Test7 t3 = new Test7();
            t3.setCol1(370);
            t3.setCol2(13747399);
            list.add(t3);
            
            Collections.sort(list);
            
            
            for(int i=0;i<list.size();i++)
            {
                System.out.println(((Test7)list.get(i)).getCol2());
            }    }}
    我的代码有点问题,就是compareTo是返回int,不是boolean..见谅啦。
      

  6.   

    //假设对象的类型
    class UserData {
        int d1, d2;
        public UserData(int n1, int n2) {
            thid.d1 = n1;
            this.d2 = n2;
        }    public void setD1(int d1) {this.d1=d1;}
        public void setD2(int d2) {this.d2=d2;}
        public int getD1() {return this.d1;}
        public int getD2() {return this.d2;}
    }//数据集合
    List<UserData> list = new ArrayList<UserData>();
    list.add(new UserData(29, 13440896));
    list.add(new UserData(791, 13745707));
    list.add(new UserData(591, 13743597));
    list.add(new UserData(591, 13743596));//排序处理(再不改动原对象的类型前提下)
    Collections.sort(list, new Comparator<UserData>() {
        public int compareTo(UserData ud1, UserData ud2) {
            if (ud1 == null) { //这里假设null为小(根据情况可以自己改null为大)
                return (ud2==null)? 0 : -1;
            } else if (ud2 == null) {
                return 1;
            } else {
                return ((ud1.getD1() == ud2.getD2()) ? 
                        (ud1.getD2()-ud2.getD2()) : (ud1.getD1()-ud2.getD2()));
            }
        }
    });for (UserData ud : list) {
        System.out.printf("%d,%d\n", ud.getD1(), ud.getD2());
    }
      

  7.   


    我用了你这个方法,但是没有排序出来,代码如下:package com.aiflow.zxb.parse;import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import com.aiflow.zxb.bean.Bean;import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;public class ParseExcel implements Comparable<Bean> {

    private int quhao;
        private int MSC_ID;
        
        
        public int getquhao() {
            return quhao;
        }    public void setquhao(int quhao) {
            this.quhao = quhao;
        }    public int getMSC_ID() {
            return MSC_ID;
        }    public void setMSC_ID(int MSC_ID) {
            this.MSC_ID = MSC_ID;
        }

    public static void main(String[] args) {
    List<Bean> data = new ArrayList<Bean>();
    List list = new ArrayList();
    try {
    // 文件路径
    InputStream is = new FileInputStream(
    "E:/需求/智能网设备码核查/标准数据/SCP设备数据汇总表.xls");
    Workbook wb = Workbook.getWorkbook(is);
    Sheet sheet = wb.getSheet(0); // 第几张表格从零开始
    String sheetName = sheet.getName();
    System.out.println("sheet表名(工作薄名): " + sheetName);
    if (sheet != null) {
    // 获取表格总列数
    int rsColumns = sheet.getColumns();
    // System.out.println("表格总列数rsColumns = " + rsColumns); // 获取表格总行数
    int rsRows = sheet.getRows();
    // System.out.println("表格总行数 rsRows = " + rsRows);
    for (int rowNum = 1; rowNum < rsRows; rowNum++) {
    Cell[] cells = sheet.getRow(rowNum);
    // for (int i = 0; i < rsColumns; i++) {
    Bean bean = new Bean();
    // System.out.println(getExcelColumnLabel(4)+"============"+cells[2].getContents());
    bean.setQuhao(Integer.parseInt(cells[2].getContents()));
    bean.setMSC_ID(Integer.parseInt(cells[4].getContents()));
    list.add(bean);
    data.add(bean);
    }//列数
    // }
    wb.close();
    }
    Collections.sort(list);
    } catch (Exception e) {
    // TODO: handle exception
    }
    for (int i = 0; i < list.size(); i++) {
    System.out.println(((Bean)list.get(i)).getMSC_ID());
    }
    }
    /**
     * 获取excel对应列的字母
     * @author zxb
     * @param num
     * @return
     */
    public static String getExcelColumnLabel(int num){
        String temp="";
        double i=Math.floor(Math.log(25.0*(num)/26.0+1)/Math.log(26))+1;
        if(i>1){
         double sub=num-26*(Math.pow(26, i-1)-1)/25;
         for(double j=i;j>0;j--){
          temp= temp+(char)(sub/Math.pow(26, j-1)+65);
          sub=sub%Math.pow(26, j-1);
         }
        }else{
         temp= temp+(char)(num+65);
        }
        return temp;
     }
    public int compareTo(Bean o) {
            // TODO Auto-generated method stub
            if(this.getMSC_ID()>o.getMSC_ID())
            {
                return 1;
            }else if(this.getMSC_ID()<o.getMSC_ID())
            {
                return -1;
            }else
            {
                return 0;
            }
    }}

      

  8.   


    比较的是list里的Bean 不是 Bean和ParseExcel 
    需要的是Bean 类实现comparable接口 
    也就是下面这个方法放到Bean里 com.aiflow.zxb.bean.Bean implements Comparable<Bean>
    public int compareTo(Bean o) {
      // TODO Auto-generated method stub
      if(this.getMSC_ID()>o.getMSC_ID())
      {
      return 1;
      }else if(this.getMSC_ID()<o.getMSC_ID())
      {
      return -1;
      }else
      {
      return 0;
      }
    }
      

  9.   

    package com.test;import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.TreeSet;public class ValueSort implements Comparable<ValueSort> {
    private int key;
    private int value;

    public int getKey() {
    return key;
    }
    public void setKey(int key) {
    this.key = key;
    }
    public int getValue() {
    return value;
    }
    public void setValue(int value) {
    this.value = value;
    }
    public ValueSort(int key,int value)
    {
    this.key=key;
    this.value=value;
    }
    public int compareTo(ValueSort o) {
    return this.value-o.value;
    }

    public static void main(String[] args) {
    ValueSort vs1=new ValueSort(129, 134);
    ValueSort vs2=new ValueSort(123, 135);
    ValueSort vs3=new ValueSort(123, 136);
    TreeSet<ValueSort> ts=new TreeSet<ValueSort>();
    ts.add(vs1);
    ts.add(vs2);
    ts.add(vs3);
    for(ValueSort s:ts)
    {
    System.out.println(s.getKey()+" "+s.getValue());
    }

    // List<ValueSort> list=new ArrayList<ValueSort>();
    // list.add(vs1);
    // list.add(vs2);
    // Collections.sort(list);
    // for(ValueSort s:list)
    // {
    // System.out.println(s.getKey()+" "+s.getValue());
    // }
    }
    }