I have read on this site inlcuding: link1 and link2 amongst others how to sort IP addresses (String representation of them) in Java. However I am not getting the correct output.
My Data (an example):
::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
I am adding these IP addresses (some valid some invalid) to an ArrayList and then passing it to this below:
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"};
//Add to ArrayList
for (String test1 : tests) {
list.add(test1);
}
//Compare and sort
Collections.sort(list);
Collections.reverse(list);
//Collections.sort(ipList, new Comparator<String>() {
for(String ip: list){
System.out.println(ip);
}
However I am not able to correctly sort the data and get incorrectly sorted results in DESCENDING ORDER. Can anyone guide me to a better way ? Thanks in advance. Please remember in ip addresses between "::" there is a zero so this is equivalent to 0:0:0
The result I get is:
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