python用户输入字符串串从小到大排序_字符串型IP地址从小到大排序

转成int再来排序吧

package org.plyy.utils;

public class IPUtil {

/**

* @param ip EG:192.168.1.1

* @return  EG:0xc0a80101

*/

public static int ipToInteger(String ip) {

int result = 0;

String ipArr[] = ip.split("\\.");

for(int i = 0; i < 4; i++) {

result |= Integer.parseInt(ipArr[i]) << ((3-i) * 8);

}

return result;

}

/**

*

* @param ip EG:0xc0a80101

* @return EG:192.168.1.1

*/

public static String intToIp(int ip) {

StringBuilder sb = new StringBuilder();

for(int i = 0; i < 4; i++) {

sb.append(String.valueOf((ip >>> (3-i) * 8) & 0xFF)).append(".");

}

sb.deleteCharAt(sb.length() - 1);

return sb.toString();

}

public static void main(String[] args) {

int result = ipToInteger("192.168.1.1");

System.out.println(Integer.toHexString(result));

System.out.println("-------------------------");

String result2 = intToIp(0xc0a80101);

System.out.println(result2);

}

}

你可能感兴趣的:(python用户输入字符串串从小到大排序_字符串型IP地址从小到大排序)