Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1458726
  • 博文数量: 187
  • 博客积分: 10375
  • 博客等级: 上将
  • 技术积分: 3127
  • 用 户 组: 普通用户
  • 注册时间: 2006-03-07 10:58
文章分类

全部博文(187)

文章存档

2013年(1)

2012年(8)

2011年(28)

2010年(36)

2009年(47)

2008年(67)

我的朋友

分类: Oracle

2010-01-05 18:22:21

Oracle中,如果非得要用SQL来对IP地址进行排序的话:
 
select ip,
to_number(substr(ip, 1, instr(ip, '.', 1, 1)-1)) newip1,
to_number(substr(ip, instr(ip,'.',1,1)+1, instr(ip, '.', 1, 2)-instr(ip,'.',1,1)-1)) newip2,
to_number(substr(ip, instr(ip,'.',1,2)+1, instr(ip, '.', 1, 3)-instr(ip,'.',1,2)-1)) newip3,
to_number(substr(ip, instr(ip,'.',1,3)+1, length(ip)+1-instr(ip,'.',1,3))) newip4
 from net_gameip
 order by newip1, newip2, newip3, newip4
 
结果:
IP                 NEWIP1     NEWIP2     NEWIP3     NEWIP4
--------------- ---------- ---------- ---------- ----------
1.2.3.8                 1          2          3          8
1.2.3.9                 1          2          3          9
1.2.3.10               1          2          3         10
1.2.3.12               1          2          3         12
1.2.3.13               1          2          3         13
1.2.3.14               1          2          3         14
1.2.3.15               1          2          3         15
1.2.3.16               1          2          3         16
1.2.3.17               1          2          3         17
1.2.3.19               1          2          3         19
1.2.3.20               1          2          3         20
1.2.3.21               1          2          3         21
1.2.3.22               1          2          3         22
1.2.3.23               1          2          3         23
2.1.1.1                 2          1          1          1
2.1.2.1                 2          1          2          1
2.2.2.2                 2          2          2          2
10.1.1.1              10          1          1          1
10.1.1.1              10          1          1          1
10.1.1.2              10          1          1          2
10.1.1.2              10          1          1          2
阅读(3373) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-01-22 09:15:03

public List getIpList() { if (ipList == null || ipList.size() <= 1) { return ipList; } Collections.sort(ipList, new Comparator() { public int compare(String ip1, String ip2) { String ipAll = ip1 + "." + ip2; String[] array = ipAll.split("\\."); int result = Integer.parseInt(array[0]) - Integer.parseInt(array[4]); if(result != 0){ return result; } result = Integer.parseInt(array[1]) - Integer.parseInt(array[5]);