0

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

名单是:

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

4 回答 4

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);
    }
于 2013-11-07T11:39:00.580 回答
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

就那么简单 !

于 2013-11-07T11:39:26.263 回答
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);
        }

    }
}

更新的解决方案......

在此处输入图像描述

于 2013-11-07T11:56:21.950 回答
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]
于 2013-11-07T12:01:17.413 回答