使用java中的相应值对重复键进行排序

时间:2013-11-07 11:35:08

标签: java sorting

我需要在项目中对门牌号码进行排序。但我没有得到我的问题的确切逻辑。

列表是:

9-11, 9-01, 10-02, 10-01, 2-09, 3-88, 9-03

我需要将上面的列表排序为:

2-09, 3-88, 9-01, 9-03, 9-11, 10-01, 10-02

我需要像上面那样的输出。

任何人都可以帮助我编写Java编码吗?

4 个答案:

答案 0 :(得分:0)

逻辑很简单:

209< 388< 901< 902< 911< 1001< 1002

一种可能的解决方案是:

    String[] input = {"9-11", "9-01", "10-02", "10-01", "2-09", "3-88", "9-03"};
    Map<Integer, String> map = new TreeMap<Integer, String>();
    for (String s : input) {
        map.put(Integer.valueOf(s.replace("-", "")), s);
    }
    TreeSet<Integer> set = new TreeSet<Integer>(map.keySet());
    String[] output = new String[input.length];
    int i = 0;
    for (Integer key : set) {
        output[i++] = map.get(key);
    }

如果您想支持更灵活的格式,可以实现Comparable以准确定义比较规则。我们来看看修改后的例子:

    class HouseNumber implements Comparable<HouseNumber>{
        private Integer house;
        private Integer flat;

        private HouseNumber(String s) {
            String[] fields = s.split("-");
            house = Integer.valueOf(fields[0]);
            flat = Integer.valueOf(fields[1]);
        }

        @Override
        public int compareTo(HouseNumber o) {
            if (house.compareTo(o.house) == 0) {
                return flat.compareTo(o.flat);
            } 
            return house.compareTo(o.house);
        }

    }

    String[] input = {"9-11", "9-01", "10-02", "10-01", "2-09", "3-88", "9-03"};
    Map<HouseNumber, String> map = new TreeMap<HouseNumber, String>();
    for (String s : input) {
        map.put(new HouseNumber(s), s);
    }
    TreeSet<HouseNumber> set = new TreeSet<HouseNumber>(map.keySet());
    String[] output = new String[input.length];
    int i = 0;
    for (HouseNumber key : set) {
        output[i++] = map.get(key);
    }

答案 1 :(得分:0)

只需从列表中删除“ - ”

即可
9-11, 9-01, 10-02, 10-01, 2-09, 3-88, 9-03

成为

911, 901, 1002, 1001, 209, 388, 903

排序

209, 388, 901, 903, 911, 1001, 1002

将“ - ”放回去,从后面跳过2个地方

2-09, 3-88, 9-01, 9-03, 9-11, 10-01, 10-02

这很简单!

答案 2 :(得分:0)

我已经分享了你解决了问题,可能会帮助你得到你想要的......

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class HouseNo {

    public HouseNo(String house, String block) {
        this.houseno = house;
        this.blockno = block;
    }

    private String houseno;
    private String blockno;

    public String getHouseno() {
        return houseno;
    }

    public void setHouseno(String houseno) {
        this.houseno = houseno;
    }

    public String getBlockno() {
        return blockno;
    }

    public void setBlockno(String blockno) {
        this.blockno = blockno;
    }

    public static class SortByHouseNo implements Comparator<HouseNo> {

        @Override
        public int compare(HouseNo o1, HouseNo o2) {
            Integer first = Integer.valueOf(o1.getHouseno());
            Integer second = Integer.valueOf(o2.getHouseno());

            Integer f1 = Integer.valueOf(o1.getBlockno());
            Integer f2 = Integer.valueOf(o2.getBlockno());

            if (first.compareTo(second) == 0) {
                return f1.compareTo(f2);
            }
            return first.compareTo(second);
        }
    }

    @Override
    public String toString() {
        return "House -> " + this.getHouseno() + "-" + this.getBlockno();
    }

    public static void main(String[] args) {
        // Final
        String houseList[] = { "9-11", "9-01", "10-02", "10-01", "2-09",
                "3-88", "9-03", "9-3" };

        HouseNo house = null;
        ArrayList<HouseNo> sortedList = new ArrayList<>();
        for (String string : houseList) {
            String h = string.substring(0, string.indexOf('-'));
            String b = string.substring(string.indexOf('-') + 1);
            house = new HouseNo(h, b);
            sortedList.add(house);
        }

        System.out.println("Before Sorting :: ");
        for (HouseNo houseNo : sortedList) {
            System.out.println(houseNo);
        }

        Collections.sort(sortedList, new SortByHouseNo());
        System.out.println("\n\nAfter Sorting HouseNo :: ");
        for (HouseNo houseNo : sortedList) {
            System.out.println(houseNo);
        }

    }
}

更新了解决方案......

enter image description here

答案 3 :(得分:0)

这样做

您的比较者

class SimpleComparator implements Comparator<String> {
    @Override
    public int compare(String o1, String o2) {
        String [] o1sub = o1.split("-");
        String [] o2sub = o2.split("-");
        int value1 = Integer.parseInt(o1sub[0]);
        int value2 = Integer.parseInt(o2sub[0]);
        if(value1!=value2){
            return new Integer (value1).compareTo(value2);
        }
        int value3 = Integer.parseInt(o1sub[1]);
        int value4 = Integer.parseInt(o2sub[1]);
        if(value3!=value4){
            return new Integer(value3).compareTo(value4);
        }
        return 0;

    }
}

数据

   String [] array ={"9-11","9-01","10-02","10-01","2-09","3-88","9-03"};

<强>分拣

 Arrays.sort(array,new SimpleComparator());     
 System.out.println(Arrays.toString(array));

<强>输出

[2-09, 3-88, 9-01, 9-03, 9-11, 10-01, 10-02]