在EXCEL中有一列是IP地址,如何按IP地址的最后一位排序??(前3位都一样)

在EXCEL中有一列是IP地址,如何按IP地址的最后一位排序??(前3位都一样),第1张

增加辅助列"后2位"列(假设在B列),
假设第一行数据在表格第二行,在单元格B2中输入公式
=rihgt(a2,2),再向下拖复制公式
最后按B列排序,升充即可

package src;
import javautilComparator;
import javautilList;
import javautilArrayList;
import javautilCollections;
class Ip { //定义ip bean
int ip1;
int ip2;
int ip3;
int ip4;
public Ip(int ip1, int ip2, int ip3, int ip4) {
thisip1 = ip1;
thisip2 = ip2;
thisip3 = ip3;
thisip4 = ip4;
}
public int getIp1() {
return ip1;
}
public void setIp1(int ip1) {
thisip1 = ip1;
}
public int getIp2() {
return ip2;
}
public void setIp2(int ip2) {
thisip2 = ip2;
}
public int getIp3() {
return ip3;
}
public void setIp3(int ip3) {
thisip3 = ip3;
}
public int getIp4() {
return ip4;
}
public void setIp4(int ip4) {
thisip4 = ip4;
}
}
class ComparatorUser implements Comparator {
public int compare(Object arg1, Object arg2) {
Ip ip1 = (Ip) arg1;
Ip ip2 = (Ip) arg2;
int flag = ip1getIp1() - ip2getIp1();
if (flag == 0) { //如果一段相等用二段排序,以此类推
flag = ip1getIp2() - ip2getIp2();
} else if (flag == 0) {
flag = ip1getIp3() - ip2getIp3();
} else if (flag == 0) {
flag = ip1getIp4() - ip2getIp4();
}
return flag;
}
}
public class Test {
public static void main(String[] args) {
List<Ip> ipList = new ArrayList<Ip>(); //定义ip List
ipListadd(new Ip(255, 2, 3, 4)); //存放ip,你的ip是从文本中读出来的我相信你会读,也会放
ipListadd(new Ip(172, 7, 8, 2));
ipListadd(new Ip(2, 3, 4, 5));
ipListadd(new Ip(2, 1, 2, 3));
ipListadd(new Ip(1, 2, 3, 4));
ipListadd(new Ip(202, 2, 3, 4));
ComparatorUser comparator = new ComparatorUser();
Collectionssort(ipList, comparator);
for (Ip ip : ipList) { //输出结果,jdk 15以上可以这么遍历list,如果你是14 就用list的长度遍历即可
Systemoutprintln(ipgetIp1() + "" + ipgetIp2() + ""
+ ipgetIp3() + "" + ipgetIp4());
}
}
}
运行结果如下
1234
2123
2345
172782
202234
255234

#include <stdioh>
#include <stdlibh>
typedef unsigned char BYTE ;
typedef struct _IP {
BYTE seg[4];
struct _IP next;
}IP;
void 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:\n192168111\n101012811\n\nInput '0000' 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;
}
}
运行结果:
Input IP address like:
192168111
101012811

Input '0000' to end input
2221582344
222158122124
19216811
1921561122
0000
Sorted ip address:
1921561122
19216811
222158122124
2221582344
Press any key to continue

如:IP为:
192168111
19216812
192168122
19216811
select
id
from
tb
order
by
parsename(id,4),parsename(id,3),parsename(id,2),parsename(id,1)
后:IP为:
19216811
192168111
19216812
192168122
如何实现,排序后:IP为:
19216811
19216812
192168111
192168122


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/13400710.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-29
下一篇 2023-07-29

发表评论

登录后才能评论

评论列表(0条)

保存