Java InetAddress Check sortAddresses(List addressList)

Here you can find the source of sortAddresses(List addressList)

Description

sort Addresses

License

Apache License

Declaration

private static void sortAddresses(List<InetAddress> addressList) 

Method Source Code

//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;
    }
}

Related

  1. isValidInetAddress(String ip)
  2. isValidInetAddress(String IP)
  3. isValidIntranetAddress(InetAddress address)
  4. isValidIpAddress(InetAddress ip)
  5. isWindowsAutoConfiguredIPv4Address(InetAddress add)