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 10:16 PM.

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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57