簡體   English   中英

用Java排序IP地址

[英]Sorting IP addresses in Java

我已經在該站點上閱讀了以下內容: link1link2等如何在Java中對IP地址(它們的字符串表示形式)進行排序。 但是我沒有得到正確的輸出。

我的數據(示例):

:: 2:3:4:5:6:7

:: 2:3:4:5:6:7:8

1 :: 8

1 :: 2:3

1 :: 2:3:4

1 :: 5:256.2.3.4

1 :: 3000.30.30.30

FE80 :: 217:f2ff:254.7.237.98,1:2:3:4 :: 5:1.2.3.4

2001:0000:1234:0000:0000:C1C0:ABCD:0876

我將這些IP地址(有些有效,有些無效)添加到ArrayList中,然后將其傳遞給以下地址:

ArrayList <String> list = new ArrayList<String>();

String [] tests = {"::2:3:4:5:6:7","2:3:4:5:6:7","::5:3:4:5:6:7:8","::5:3:4:5:6:7:8:9:0","1::8","1::2:3","1::2:3:4","1::5:256.2.3.4","1:1:3000.30.30.30","ae80::217:f2ff:254.7.237.98,1:2:3:4::5:1.2.3.4","2001:0000:1234:0000:0000:C1C0:ABCD:0876",
     "12345::6:7:8","1::1.2.900.4","fe80::","::ffff:0:0"};

//添加到ArrayList

    for (String test1 : tests) {
    list.add(test1);
    }

//比較和排序

 Collections.sort(list);
    Collections.reverse(list);

    //Collections.sort(ipList, new Comparator<String>() {
    for(String ip: list){
    System.out.println(ip);
}

但是,我無法正確排序數據並在DESCENDING ORDER中得到錯誤排序的結果。 誰能引導我找到更好的方法? 提前致謝。 請記住,“ ::”之間的IP地址中有一個零,因此它等效於0:0:0

我得到的結果是:

FE80 ::

ae80 :: 217:f2ff:254.7.237.98,1:2:3:4 :: 5:1.2.3.4

:: FFFF:0:0

:: 5:3:4:5:6:7:8:9:0

:: 5:3:4:5:6:7:8

:: 2:3:4:5:6:7

2:3:4:5:6:7

2001:0000:1234:0000:0000:C1C0:ABCD:0876

1 :: 8

1 :: 5:256.2.3.4

1 :: 2:3:4

1 :: 2:3

1 :: 1.2.900.4

1:1:3000.30.30.30

12345 :: 6:7:8

嘗試這個

Test.java

import java.util.*;

public class Test
{
    public static void main(String[] args)
    {
        ArrayList <String> list = new ArrayList<String>();
        String [] tests = {"::2:3:4:5:6:7","2:3:4:5:6:7","::5:3:4:5:6:7:8","::5:3:4:5:6:7:8:9:0","1::8","1::2:3","1::2:3:4","1::5:256.2.3.4","1:1:3000.30.30.30","ae80::217:f2ff:254.7.237.98","1:2:3:4::5:1.2.3.4","2001:0000:1234:0000:0000:C1C0:ABCD:0876","12345::6:7:8","1::1.2.900.4","fe80::","::ffff:0:0"};
        for (String test1 : tests)
        {
            list.add(test1);
        }

        System.out.println();
        System.out.println("Ascending Order");

        Collections.sort(list, new AlphanumComparator());
        for(String ip: list)
        {
            System.out.println(ip);
        }

        System.out.println();
        System.out.println("Descending Order");

        Collections.reverse(list);
        for(String ip: list)
        {
            System.out.println(ip);
        }
    }
}

AlphanumComparator.java

import java.util.Comparator;

public class AlphanumComparator implements Comparator
{
    private final boolean isDigit(char ch)
    {
        return ch >= 48 && ch <= 57;
    }

    private final String getChunk(String s, int slength, int marker)
    {
        StringBuilder chunk = new StringBuilder();
        char c = s.charAt(marker);
        chunk.append(c);
        marker++;
        if (isDigit(c))
        {
            while (marker < slength)
            {
                c = s.charAt(marker);
                if (!isDigit(c))
                    break;
                chunk.append(c);
                marker++;
            }
        } else
        {
            while (marker < slength)
            {
                c = s.charAt(marker);
                if (isDigit(c))
                    break;
                chunk.append(c);
                marker++;
            }
        }
        return chunk.toString();
    }

    public int compare(Object o1, Object o2)
    {
        if (!(o1 instanceof String) || !(o2 instanceof String))
        {
            return 0;
        }
        String s1 = (String)o1;
        String s2 = (String)o2;

        int thisMarker = 0;
        int thatMarker = 0;
        int s1Length = s1.length();
        int s2Length = s2.length();

        while (thisMarker < s1Length && thatMarker < s2Length)
        {
            String thisChunk = getChunk(s1, s1Length, thisMarker);
            thisMarker += thisChunk.length();

            String thatChunk = getChunk(s2, s2Length, thatMarker);
            thatMarker += thatChunk.length();

            int result = 0;
            if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))
            {
                int thisChunkLength = thisChunk.length();
                result = thisChunkLength - thatChunk.length();
                if (result == 0)
                {
                    for (int i = 0; i < thisChunkLength; i++)
                    {
                        result = thisChunk.charAt(i) - thatChunk.charAt(i);
                        if (result != 0)
                        {
                            return result;
                        }
                    }
                }
            } else
            {
                result = thisChunk.compareTo(thatChunk);
            }

            if (result != 0)
                return result;
        }

        return s1Length - s2Length;
    }
}

這給您訂單。 希望對您有所幫助

有序IP

嘗試這個。

static String normalizeIP(String s) {
    try {
        byte[] b = InetAddress.getByName(s).getAddress();
        StringBuilder sb = new StringBuilder();
        for (byte e : b)
            sb.append(String.format("%03d", Byte.toUnsignedInt(e)));
        return sb.toString();
    } catch (UnknownHostException e) {
        return s;
    }
}

public static void main(String[] args) {
    String[] ads = {
        "::2:3:4:5:6:7",
        "::2:3:4:5:6:7:8",
        "1::8",
        "1::2:3",
        "1::2:3:4",
        "1::5:255.2.3.4",
        "fe80::217:f2ff:254.7.237.98",
        "1:2:3:4::5:1.2.3.4",
        "2001:0000:1234:0000:0000:C1C0:ABCD:0876",};
    Arrays.sort(ads, (a, b) -> normalizeIP(b).compareTo(normalizeIP(a)));
    for (String s : ads)
        System.out.println(s);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM