sorting string array based on delimiter

Joined
Aug 9, 2010
Messages
1
Reaction score
0
Hi ,

I need to sort a string array based on delimiter

example,
have below values in string array
cproj@@9.184.184.143@@mycon2@@dbo@@ABC,
cproj@@9.184.184.143@@mycon2@@dbo@@B,
cproj@@9.184.184.143@@mycon2@@dbo@@Ctable,
cproj@@9.184.184.143@@mycon2@@dbo@@ZZ,
cproj@@9.184.184.143@@mycon2@@dbo@@a,
cproj@@9.184.184.143@@mycon2@@dbo@@katble,
cproj@@9.184.184.143@@mycon2@@dbo@@mtable,
cproj@@9.184.184.143@@mycon2@@dbo@@ztable

I need output in sorted way case insensitive
cproj@@9.184.184.143@@mycon2@@dbo@@a,
cproj@@9.184.184.143@@mycon2@@dbo@@ABC,
cproj@@9.184.184.143@@mycon2@@dbo@@B,
cproj@@9.184.184.143@@mycon2@@dbo@@Ctable,
cproj@@9.184.184.143@@mycon2@@dbo@@katble,
cproj@@9.184.184.143@@mycon2@@dbo@@mtable,
cproj@@9.184.184.143@@mycon2@@dbo@@ZZ,
cproj@@9.184.184.143@@mycon2@@dbo@@ztable
 
Joined
Aug 15, 2010
Messages
2
Reaction score
0
Um...I recommend creating a compareTo method in a comparator that takes in the string and takes out the delimeters. Then you can just use Arrays.sort(ArrayName, ComparatorName).

Ex:
public DelimeterComparator implements Comparator<String>
{
public int compare(String anotherString, String aString){
while(x>=0 && y>=0){
int x=anotherString.indexOf(DELIMETER);
int y=aString.indexOf(DELIMETER);
if (x>=0 && y>=0){
tempString1=anotherString.substring(0,x);
tempString2=aString.substring(0,y);
if (!tempString1.equals(tempString2))
//Returns negative # if tempString1<tempString2, else positive number
return tempString1.compareTo(tempString2);
else{
anotherString=anotherString.subString(x+DELIMETER.length());
aString=aString.subString(y+DELIMETER.length());
}
}
}
//Strings are equal
return 0;
}
private static final DELIMETER="@@";
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top