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

   private void sort( List valueList,List nameList) {
        List xValue = new ArrayList<>();
        List xName = new ArrayList<>();
        int de;
        int i = 0;
        Iterator 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 yValue = new ArrayList<>();
        List 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 xValue = new ArrayList<>();
        List xName = new ArrayList<>();

        int de;
        int i = 0;
        Iterator 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);
        }

    }

排序前后:

java对两个有对应关系的list进行排序_第1张图片

你可能感兴趣的:(java基础)