Here you can find the source of sortAddresses(List
private static void sortAddresses(List<InetAddress> addressList)
//package com.java2s; //License from project: Apache License import java.net.InetAddress; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { private static void sortAddresses(List<InetAddress> addressList) { Collections.sort(addressList, new Comparator<InetAddress>() { @Override//from w ww .jav a 2 s . c o m public int compare(InetAddress o1, InetAddress o2) { return compareBytes(o1.getAddress(), o2.getAddress()); } }); } private static int compareBytes(byte[] left, byte[] right) { for (int i = 0, j = 0; i < left.length && j < right.length; i++, j++) { int a = (left[i] & 0xff); int b = (right[j] & 0xff); if (a != b) { return a - b; } } return left.length - right.length; } }