Java中的IPRange Sorter

时间:2017-02-20 04:26:34

标签: java

如何对java中的iprange进行排序

例如:

我的输入是一串IpRange地址,即

9.9.9.9/12
100.0.0.0/12
8.8.8.8/12
1.2.3.4/32
1.2.2.2/12
12.3.1.45/12

我的输出应该是

1.2.2.2/12
1.2.3.4/32
8.8.8.8/12
9.9.9.9/12
12.3.1.45/12
100.0.0.0/12

TIA

1 个答案:

答案 0 :(得分:-1)

您可以编写自己的比较器。看起来很容易。这也在blogs中共享。以下是非常简单的实施(仅供参考),

public class TestIPSorter {
   @Test
    public void test() {
        String[] input = {"9.9.9.9/12" ,"100.0.0.0/12" ,"8.8.8.8/12" ,"1.2.3.4/32" ,"1.2.2.2/12", "12.3.1.45/12"};
        String[] expectedOutput = {"1.2.2.2/12" ,"1.2.3.4/32", "8.8.8.8/12" ,"9.9.9.9/12" ,"12.3.1.45/12", "100.0.0.0/12"};


        String[] sortedIp = IPSorter.sort(input);

        Assert.assertArrayEquals(expectedOutput, sortedIp);
    }
}


public class IPSorter {

    public static String[] sort(String[] input) {
        IpAddress[] array = Arrays.stream(input).map(i -> IpAddress.valueOf(i)).toArray(IpAddress[]::new);
        Arrays.sort(array);
        return Arrays.stream(array).map(ipAd -> ipAd.getAddress()).toArray(String[]::new);
    }

    private static class IpAddress implements Comparable<IpAddress> {

        private final String original;
        private final String ipStart;
        private final int to;

        private IpAddress(String address) {
            int forwardSlash = address.lastIndexOf("/");
            ipStart = address.substring(0, forwardSlash);
            to = Integer.parseInt(address.substring(forwardSlash + 1));
            original = address;
        }

        static IpAddress valueOf(String address) {
            return new IpAddress(address);
        }

        String getAddress() {
            return original;
        }

        @Override
        public int compareTo(IpAddress o) {
            try {
                byte[] ba1 = InetAddress.getByName(this.ipStart).getAddress();
                byte[] ba2 = InetAddress.getByName(o.ipStart).getAddress();

                // general ordering: ipv4 before ipv6
                if (ba1.length < ba2.length)
                    return -1;
                if (ba1.length > ba2.length)
                    return 1;

                // we have 2 ips of the same type, so we have to compare each byte
                for (int i = 0; i < ba1.length; i++) {
                    int b1 = unsignedByteToInt(ba1[i]);
                    int b2 = unsignedByteToInt(ba2[i]);
                    if (b1 == b2)
                        continue;
                    if (b1 < b2) {
                        return -1;
                    } else {
                        return 1;
                    }
                }
                return compareRange(to, o.to);
            } catch (UnknownHostException e) {
                throw new RuntimeException(e);
            }
        }

        private int compareRange(int range1, int range2) {
            return Integer.compare(range1, range2);


        }

        private int unsignedByteToInt(byte b) {
            return (int) b & 0xFF;
        }
    }


}