![]() |
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 |
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.