java对两个有对应关系的list进行排序

   private void sort( List<Double> valueList,List<String> nameList) {
        List<Double> xValue = new ArrayList<>();
        List<String> xName = new ArrayList<>();
        int de;
        int i = 0;
        Iterator<Double> it = valueList.iterator();
        while(it.hasNext()){
            de = 0;
            for(int j = 0; j < valueList.size(); j ++) {
                if(valueList.get(de) < valueList.get(j)) {
                    de = j;
                }
            }
            xValue.add(i,valueList.get(de));
            valueList.remove(de);
            xName.add(i,nameList.get(de));
            nameList.remove(de);
            i++;
        }
        valueList = xValue;
        nameList = xName;
    }

直观的:

  public static  void main(String []fsdsf) {
        List<Double> yValue = new ArrayList<>();
        List<String> yName = new ArrayList<>();
        yValue.add(9.0);
        yName.add("aa");

        yValue.add(12.0);
        yName.add("bb");

        yValue.add(3.0);
        yName.add("cc");

        yValue.add(7.0);
        yName.add("dd");

        List<Double> xValue = new ArrayList<>();
        List<String> xName = new ArrayList<>();

        int de;
        int i = 0;
        Iterator<Double> it = yValue.iterator();
        while(it.hasNext()){
            de = 0;
            for(int j = 0; j < yValue.size(); j ++) {
                if(yValue.get(de) < yValue.get(j)) {
                    de = j;
                }
            }
            xValue.add(i,yValue.get(de));
            yValue.remove(de);
            xName.add(i,yName.get(de));
            yName.remove(de);
            i++;
        }

        for (Double d : xValue ) {
            System.out.println(d);
        }

        for (String d : xName ) {
            System.out.println(d);
        }

    }

排序前后:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值