Answered # help sorting ip address

masijade 1,351 gunjannigam 28 Discussion Starter ardi_lucy Featured Reply JamesCherrill 2,785 camocspro anita_2 Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

help me plz i need to sort ip address, am try to sort

in java.util.Arrays.sort() but it not correct for eg

```
String st[]={"10.4.23.16","10.4.23.9"};
java.util.Arrays.sort(st);
for(int i=0;i<st.length;i++){
System.out.println(st[i]);
}
```

the out put is

10.4.23.16

10.4.23.9

plz give idea

*Edited 6 Years Ago by ardi_lucy*: n/a

0

Yes. Write a Comparator. (see the API docs for Comparator and the Collections Tutorials)

0

help me plz i need to sort ip address, am try to sort

in java.util.Arrays.sort() but it not correct for eg`String st[]={"10.4.23.16","10.4.23.9"}; java.util.Arrays.sort(st); for(int i=0;i<st.length;i++){ System.out.println(st[i]); }`

the out put is

10.4.23.16

10.4.23.9

plz give idea

The Algo which you a using will sort the string array. For comparing strings it will compare each compare starting from 1st character. Now for the eg given both string are same until 8 characters(start counting from 1), i.e '.' . Now for second IP 9th character is 9 and for 1st IP its 1 so as 9 is bigger the bigger number is at the bottom of list.

So if you want to still use this algo u can stuff your IPaddress with extra zeros, such that their three digits after each '.'

Or you can construct your own comparator which will separate the all the no after "." then compare all the nos accordingly.

0

help me plz i need to sort ip address, am try to sort

in java.util.Arrays.sort() but it not correct for eg`String st[]={"10.4.23.16","10.4.23.9"}; java.util.Arrays.sort(st); for(int i=0;i<st.length;i++){ System.out.println(st[i]); }`

the out put is

10.4.23.16

10.4.23.9

plz give idea

10q for ur replies i will try to make my own comparator.

1

Take a look at the InetAddress class. You can use it to parse your text into an internal-form inet address

InetAddress.getByName("10.4.23.16")

then extract it as a byte array

byte[] bytes = InetAddress.getByName("10.4.23.16").getAddress();

It will then be easy to write the comparator using those four numeric bytes (four nested tests OR bit-shift them into a single int and compare that).

0

You cannot use util.sort because the string is an ip and sorting wont be right as it is not a string.

Here is a code I've used to sort ip address properly. I've used StringTokenizer to break down the ip address and store it in a two dimensional array. Then i used a modified bubble sort algorithm to sort the ip address

```
import java.util.StringTokenizer;
public class IPsort {
public static void main(String[] args) {
String ipstring[]={"10.4.23.16","10.4.23.9","255.255.255.0","10.40.23.16","11.4.23.16","243.255.255.1"};
String [] SortedIp = new String[ipstring.length];
int [][] a = new int[ipstring.length][4];
for(int cntr =0;cntr<ipstring.length;cntr++){
StringTokenizer st1 = new StringTokenizer(ipstring[cntr], ".");
//iterate through tokens
int i = 0;
while(st1.hasMoreTokens()){
a[cntr][i] = Integer.parseInt(st1.nextToken());
i++;
}
}
int pass, in, temp;
for (pass=1; pass < ipstring.length; pass++) { // count how many times
//System.out.println("pass : " + pass);
// This next loop becomes shorter and shorter
for (in=0; in < (ipstring.length) - pass; in++) {
//System.out.println("in value is : " +in);
if(a[in][0] > a[in+1][0]) // out of order?
{
temp = a[in][0];
a[in][0] = a[in+1][0];
a[in+1][0] = temp;
//swap(in, in+1); // swap them
temp = a[in][1];
a[in][1] = a[in+1][1];
a[in+1][1] = temp;
//swap(in, in+1); // swap them
temp = a[in][2];
a[in][2] = a[in+1][2];
a[in+1][2] = temp;
//swap(in, in+1); // swap them
//swap(in, in+1); // swap them
temp = a[in][3];
a[in][3] = a[in+1][3];
a[in+1][3] = temp;
//swap(in, in+1); // swap them
}
if(a[in][0] == a[in+1][0])
{
if(a[in][1] > a[in+1][1]) // out of order?
{
temp = a[in][0];
a[in][0] = a[in+1][0];
a[in+1][0] = temp;
//swap(in, in+1); // swap them
temp = a[in][1];
a[in][1] = a[in+1][1];
a[in+1][1] = temp;
//swap(in, in+1); // swap them
temp = a[in][2];
a[in][2] = a[in+1][2];
a[in+1][2] = temp;
//swap(in, in+1); // swap them
//swap(in, in+1); // swap them
temp = a[in][3];
a[in][3] = a[in+1][3];
a[in+1][3] = temp;
//swap(in, in+1); // swap them
}
if(a[in][1] == a[in+1][1]) // out of order?
{
if(a[in][2] > a[in+1][2])
{
temp = a[in][0];
a[in][0] = a[in+1][0];
a[in+1][0] = temp;
//swap(in, in+1); // swap them
temp = a[in][1];
a[in][1] = a[in+1][1];
a[in+1][1] = temp;
//swap(in, in+1); // swap them
temp = a[in][2];
a[in][2] = a[in+1][2];
a[in+1][2] = temp;
//swap(in, in+1); // swap them
//swap(in, in+1); // swap them
temp = a[in][3];
a[in][3] = a[in+1][3];
a[in+1][3] = temp;
//swap(in, in+1); // swap them
}
if(a[in][2] == a[in+1][2])
{
if(a[in][3] > a[in+1][3])
{
temp = a[in][0];
a[in][0] = a[in+1][0];
a[in+1][0] = temp;
//swap(in, in+1); // swap them
temp = a[in][1];
a[in][1] = a[in+1][1];
a[in+1][1] = temp;
//swap(in, in+1); // swap them
temp = a[in][2];
a[in][2] = a[in+1][2];
a[in+1][2] = temp;
//swap(in, in+1); // swap them
//swap(in, in+1); // swap them
temp = a[in][3];
a[in][3] = a[in+1][3];
a[in+1][3] = temp;
//swap(in, in+1); // swap them
}
}
}
}
}
}
for(int cntr = 0; cntr<ipstring.length;cntr++)
{
for(int i=0;i<4;i++)
{
if(i==0){
SortedIp[cntr] = Integer.toString(a[cntr][i]);
}
else{
SortedIp[cntr] = SortedIp[cntr] +"." +Integer.toString(a[cntr][i]);
}
}
}
for (int cntr = 0; cntr<ipstring.length;cntr++)
{
System.out.println("Ip's given :-"+ ipstring[cntr]);
}
for (int cntr = 0; cntr<ipstring.length;cntr++)
{
System.out.println("Ip sorted is :-"+ SortedIp[cntr]);
}
}
}
```

**OUTPUT :***Ip's given :-10.4.23.16
Ip's given :-10.4.23.9
Ip's given :-255.255.255.0
Ip's given :-10.40.23.16
Ip's given :-11.4.23.16
Ip's given :-243.255.255.1
Ip sorted is :-10.4.23.9
Ip sorted is :-10.4.23.16
Ip sorted is :-10.40.23.16
Ip sorted is :-11.4.23.16
Ip sorted is :-243.255.255.1
Ip sorted is :-255.255.255.0*

0

I have sort ip addressess

input like this-

19.19.18.0/24

19.19.19.0/24

18.0.0.0/8

198.19.0.0/16

198.19.19.0/24

199.19.19.0/24

want output like

18.0.0.0/8

198.19.0.0/16

198.19.19.0/24

199.19.19.0/24

19.19.18.0/24

19.19.19.0/24

i used this but not working properly

```
public int compare(WSAPolicy NwsaPolicy1, WSAPolicy NwsaPolicy2)
{
byte[] ba1 = NwsaPolicy1.getIdentity().getName().trim().getBytes();
byte[] ba2 = NwsaPolicy2.getIdentity().getName().trim().getBytes();
if(ba1.length < ba2.length) return -1;
if(ba1.length > ba2.length) return 1;
for(int i = 0; i < ba1.length; i++) {
int b1 = unsignedByteToInt(ba1[i]);
int b2 = unsignedByteToInt(ba2[i]);
if(b1 == b2)
continue;
if(b1 < b2)
return 1;
else
return -1;
}
return 0;
}
private int unsignedByteToInt(byte b) {
return (int) b & 0xFF;
}
```

This question has already been answered. Start a new discussion instead.

Recommended Articles

Hello All ...

Iam Getting An Error With try to excecute the stored procedure .

I have Have Sql database , the stored procedure like so :

```
USE [MPRS]
GO
/****** Object: StoredProcedure [dbo].[Search_Licenses_By_Number] Script Date: 26-Nov-16 8:06:52 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE ...
```

Help! I want to create a java program that finds the highest even integer among the values entered by the user. Stop asking values when a value less than 1 have been entered. If no even integer is entered, display "No Even Integer"

Here is the sample output that I ...