1. C ++ 版本的
主要是使用了 一个结构体来表示ip,
struct _ip
{
char seg[4];//char 的大小刚好符合ip的一个10进制表示
_ip * next;
};
排序采用的是类似插入排序,关键词使用的是ip的每一个点分表示
#include
#include
typedef unsigned char BYTE ;
typedef struct _IP {
BYTE seg[4];
struct _IP *next;
}IP;
int main()
{
int i, found;
IP *pip, *ptem, *pfind;
BYTE tem_ip[4];
pip = (IP *)malloc(sizeof(IP));
pip->next = pip;
printf("Input IP address like:\n192.168.1.11\n10.10.128.11\n......\nInput '0.0.0.0' to end input\n");
//类似一个插入排序的过程
while (1) {
scanf("%d.%d.%d.%d", &tem_ip[0], &tem_ip[1], &tem_ip[2], &tem_ip[3]);
if (!(tem_ip[0] || tem_ip[1] || tem_ip[2] || tem_ip[3]))
break;
ptem = (IP *)malloc(sizeof(IP));
for (i = 0; i < 4; i++)
ptem->seg[i] = tem_ip[i];
found = 0;
pfind = pip;
while (pfind->next != pip) {
for (i = 0; i < 4; i++) {
if (ptem->seg[i] < pfind->next->seg[i]) {
found = 1;
break;
} else if (ptem->seg[i] > pfind->next->seg[i]) {
break;
}
}
if (found)
break;
pfind = pfind->next;
}
ptem->next = pfind->next;
pfind->next = ptem;
}
printf("\nSorted ip address:\n");
ptem = pip->next;
while (ptem != pip) {
printf("%d.%d.%d.%d\n", ptem->seg[0], ptem->seg[1], ptem->seg[2], ptem->seg[3]);
ptem = ptem->next;
}
return 0;
}
2. Java 版本的排序,这个自定义的排序也是使用了 compareTo函数,这里ip的表示使用的就是简单的字符串表示
import java.util.*;
public class TestIpSort {
public static void main(String[] args){
//System.out.println(Arrays.toString(IP.class.getDeclaredFields()));
IP ip5=new IP("10.100.0.0","fdfd");
IP ip6=new IP("10.50.0.0","test");
//System.out.println(ip5.getIP().compareTo(ip6.getIP()));
IP ip4=new IP("10.4.120.50","cdf");
IP ip1=new IP("10.4.120.99","def");
IP ip2=new IP("10.4.120.101","bbf");
IP ip3=new IP("10.4.120.100","abc");
List list=new ArrayList();
list.add(ip1);
list.add(ip2);
list.add(ip3);
list.add(ip4);
list.add(ip5);
list.add(ip6);
//System.out.println("排序前:"+list);
Collections.sort(list,IP.IPComparator);
System.out.println("IP正序:"+list);
//Collections.sort(list,IP.nameComparator);
//System.out.println("NAME正序:"+list);
}
}
class IP {
IP(String IP,String name){
this.IP=IP;
this.name=name;
}
//private T sortField;
private String IP;
private String name;
public static Comparator IPComparator=new Comparator(){
@Override
public int compare(Object arg0, Object arg1) {
IP ip1=(IP)arg0;
IP ip2=(IP)arg1;
return compartTo(ip1.IP,ip2.IP);
}
};
public static Comparator nameComparator=new Comparator(){
@Override
public int compare(Object arg0, Object arg1) {
IP ip1=(IP)arg0;
IP ip2=(IP)arg1;
return ip1.name.compareTo(ip2.name);
}
};
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;
}
public static int compartTo(String ip1,String ip2){
//以下方法不能判断的原因在于:
例如10.4.120.5与10.50.0.0按理应该前者小,但将它们转化为数字组合后,后者位数少,所以反而变成后面一个数字更小。
//String ip11=ip1.replace(".","");
//String ip22=ip2.replace(".", "");
//return new Long(ip11).compareTo(new Long(ip22));
//比较2个IP的顺序,按照数字顺序
long[] ip11=parseIp(ip1);
long[] ip22=parseIp(ip2);
long ip1Result=0,ip2Result=0;
for(int i=0;i<4;i++){
ip1Result+=(ip11[i]<
}
for(int i=0;i<4;i++){
ip2Result+=(ip22[i]<
}
if(ip1Result-ip2Result>0){
return 1;
}else if(ip1Result-ip2Result<0){
return -1;
}else{
return 0;
}
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString(){
return this.name+"-"+this.IP;
}
public String getIP() {
return IP;
}
public void setIP(String iP) {
IP = iP;
}
}