Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   sorting string array based on delimiter (http://www.velocityreviews.com/forums/t730432-sorting-string-array-based-on-delimiter.html)

padmagvs 08-09-2010 09:47 AM

sorting string array based on delimiter
 
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

cuffJ 08-15-2010 11:21 PM

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="@@";
}


All times are GMT. The time now is 12:00 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.