IP 地址排序Java

比如我有个文本文件 ip.txt,里面有如下 IP 地址:

255.2.3.4
172.7.8.2
192.10.8.9
192.10.8.7
2.3.4.5
2.4.5.6
1.2.3.45

怎么能够重新排序,先按第1位数;第1位相同,按第2位; 如此类推...从小到大

package src;

import java.util.Comparator;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;

class Ip { //定义ip bean
int ip1;

int ip2;

int ip3;

int ip4;

public Ip(int ip1, int ip2, int ip3, int ip4) {
this.ip1 = ip1;
this.ip2 = ip2;
this.ip3 = ip3;
this.ip4 = ip4;
}

public int getIp1() {
return ip1;
}

public void setIp1(int ip1) {
this.ip1 = ip1;
}

public int getIp2() {
return ip2;
}

public void setIp2(int ip2) {
this.ip2 = ip2;
}

public int getIp3() {
return ip3;
}

public void setIp3(int ip3) {
this.ip3 = ip3;
}

public int getIp4() {
return ip4;
}

public void setIp4(int ip4) {
this.ip4 = ip4;
}

}

class ComparatorUser implements Comparator {

public int compare(Object arg1, Object arg2) {
Ip ip1 = (Ip) arg1;
Ip ip2 = (Ip) arg2;

int flag = ip1.getIp1() - ip2.getIp1();
if (flag == 0) { //如果一段相等用二段排序,以此类推
flag = ip1.getIp2() - ip2.getIp2();
} else if (flag == 0) {
flag = ip1.getIp3() - ip2.getIp3();
} else if (flag == 0) {
flag = ip1.getIp4() - ip2.getIp4();
}
return flag;
}

}

public class Test {
public static void main(String[] args) {
List<Ip> ipList = new ArrayList<Ip>(); //定义ip List
ipList.add(new Ip(255, 2, 3, 4)); //存放ip,你的ip是从文本中读出来的.我相信你会读,也会放.
ipList.add(new Ip(172, 7, 8, 2));
ipList.add(new Ip(2, 3, 4, 5));
ipList.add(new Ip(2, 1, 2, 3));
ipList.add(new Ip(1, 2, 3, 4));
ipList.add(new Ip(202, 2, 3, 4));

ComparatorUser comparator = new ComparatorUser();
Collections.sort(ipList, comparator);
for (Ip ip : ipList) { //输出结果,jdk 1.5以上可以这么遍历list,如果你是1.4 就用list的长度遍历即可
System.out.println(ip.getIp1() + "." + ip.getIp2() + "."
+ ip.getIp3() + "." + ip.getIp4());
}
}
}
运行结果如下.
1.2.3.4
2.1.2.3
2.3.4.5
172.7.8.2
202.2.3.4
255.2.3.4
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-08-17
import java.util.Arrays;

public class IPAddressSort {
public static void main(String[] args) {
String[] ip = { "255.2.3.4", "172.7.8.2", "192.10.8.9", "192.10.8.7", "2.3.4.5", "2.4.5.6", "1.2.3.45" };
ip = fill(ip); //先填充ip地址,前面补0
Arrays.sort(ip); //调用Arrays里的sort(Object[] o)方法进行排序
ip = recovery(ip); //回复ip地址,去除补上的0
for (String s : ip) { //输出效果
System.out.println(s);
}
}

static String[] fill(String[] ip) {
for (int i = 0; i < ip.length; i++) {
String[] temp = ip[i].split("\\."); //字符串分组split()方法里是填正则表达式。所用用“\\.”
ip[i] = ""; //先设为空字符
for (int j = 0; j < temp.length; j++) {
if (Integer.parseInt(temp[j]) / 10 == 0) { //当该ip段为0-9时,前面补两个0
temp[j] = "00" + temp[j];
} else if (Integer.parseInt(temp[j]) / 100 == 0) { //当该ip段为10-99时,前面补一个0
temp[j] = "0" + temp[j];
}
ip[i] += temp[j] + "."; //重新赋值
}
ip[i] = ip[i].substring(0, ip[i].length() - 1); //去除最后一个多余的“.”
}
return ip;
}

static String[] recovery(String[] ip) {
for (int i = 0; i < ip.length; i++) {
String[] temp = ip[i].split("\\.");
ip[i] = "";
for (int j = 0; j < temp.length; j++) {
if (temp[j].startsWith("00")) { //去除0-9前面的0,一定要放前面
temp[j] = temp[j].substring(2);
} else if (temp[j].startsWith("0")) { //去除10-99前面的0,一定要放后面
temp[j] = temp[j].substring(1);
}
ip[i] += temp[j] + ".";
}
ip[i] = ip[i].substring(0, ip[i].length() - 1);
}
return ip;
}
}

//用2L的思路实现的。这个思路比较好。追问

--分就给你吧,同时也感谢AiDirac--想得周全;只是所有答案都太繁琐了,我用Ruby解决了,代码如下:

ip_array = []

IO.foreach("ip.txt") { |line|
line.chomp
ip_array << line.split(/\./).map!{ |o| o.to_i }.pack("C*")
}

ip_array.sort.each { |ip|
print ip.unpack("C*").join("."), "\n"
}

本回答被提问者采纳
第2个回答  2011-11-11
ip.txt就按LZ给的,排序完成后在控制台会输出排序后的,ip.txt里面也会变成排序后的结果,
完整代码(请看注释):
//Test.java
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Test {
public static void main(String[] args) throws IOException {
//读入ip.txt文件 我放C盘下面,你可以自己定义位置
BufferedReader br = new BufferedReader(new FileReader("c:/ip.txt"));
List <String> list = new ArrayList<String>();
String str;
//将每条ip都读入list(ip.txt里面,一条IP一行)
while((str=br.readLine())!=null)
list.add(str);

//采用IPComparator的算法进行排序
Collections.sort(list,IPComparator);

//控制输出一下,如果不需要 你可以去掉
for(Object o :list)
System.out.println((String)o);
br.close();

//把排序好的,输出回ip.txt
BufferedWriter bw = new BufferedWriter(new FileWriter("c:/ip.txt"));
for(Object o:list)
{
bw.write((String)o+"\r\n");
bw.flush();
}
bw.close();
}

//后面的内容都是IPComparator算法的东西。
public static int compartTo(String ip1,String ip2){
long[] ip11=parseIp(ip1);
long[] ip22=parseIp(ip2);
long ip1Result=0,ip2Result=0;
for(int i=0;i<4;i++){
ip1Result+=(ip11[i]<<(24-i*8));
}
for(int i=0;i<4;i++){
ip2Result+=(ip22[i]<<(24-i*8));
}
if(ip1Result-ip2Result>0){
return 1;
}else if(ip1Result-ip2Result<0){
return -1;
}else{
return 0;
}
}

public static Comparator IPComparator=new Comparator(){
public int compare(Object ip1, Object ip2) {
return compartTo((String)ip1,(String)ip2);
}
};

private static long[] parseIp(String ip){
ip=ip.replace(".", "#");
long result[]=new long[4];
String[] ip1=ip.split("#");
if(ip!=null){
result[0]=Long.parseLong(ip1[0]);
result[1]=Long.parseLong(ip1[1]);
result[2]=Long.parseLong(ip1[2]);
result[3]=Long.parseLong(ip1[3]);
}
return result;
}

}
第3个回答  2011-11-11
建议这样做,将ip地址的每一段都填充为三位(两位和一位的用零填充),删除地址中的'.',然后导入到Arrays,使用sort()方法进行排序
第4个回答  2011-11-11
按"."截取为四段字符串~将每段转为整型也就是数字,再将整型变量,按你需求进行比较排序即可~