Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > sorting string array based on delimiter

Reply
Thread Tools

sorting string array based on delimiter

 
 
padmagvs padmagvs is offline
Junior Member
Join Date: Aug 2010
Posts: 1
 
      08-09-2010
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
 
Reply With Quote
 
 
 
 
cuffJ cuffJ is offline
Junior Member
Join Date: Aug 2010
Posts: 3
 
      08-15-2010
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="@@";
}
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem while splitting the line using a delimiter and pushing itinto an array Prasanth C++ 4 11-22-2008 05:10 AM
convert string to array with delimiter Valentino Lun Ruby 3 11-05-2008 09:16 AM
Reading an array of byte with special delimiter....help me!! gbattine Java 0 06-19-2006 11:27 AM
ASP Form Array - Delimiter Problem rhungund@gmail.com ASP General 2 04-06-2005 04:15 PM
Parse Array of Objects - string delimiter java Pedro Rocha Java 8 12-08-2004 08:54 AM



Advertisments