• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need Help with Sorting a split string  RSS feed

 
justin e wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here a example I have a string formated like this in a string call holder4 "john ;17868|jim;19093|luke;12098"

I need to sort it out from the least number to the greatest so first I split the string by the "|" so I have

john ;17868 in tokens[0]
jim;19093 in tokens[1]
luke;1209 tokens[2]

String phrase = holder4;
String delims = "[|]";
String[] tokens = phrase.split(delims);

but now I need to some how sort them by the least highest numbers after the ";" in each of the tokens and reconstruct a new string that look like this "luke;1209|john ;17868|jim;19093"

thank you in advance for any help
 
justin e wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It for my android program but I guess it would be more java-specific. thank you
Bear Bibeault wrote:It doesn't look to me like there's anything Android-specific in this question. It's been moved to a more appropriate location.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
justin e wilson wrote:thank you in advance for any help

Well, there are a few ways to do it:
1. (Probably easiest) Set up a Comparator that compares your strings by the numeric portion and then run Arrays.sort() on it.
2. Use delims = "[|;]" for your split(). That will create separate entries for the alpha and numeric portions which you can then extract and sort as you see fit.
3. Change the order of your input so that the number part comes first.

Personally, I'd go with 1.

Winston
 
justin e wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
justin e wilson wrote:thank you in advance for any help

Well, there are a few ways to do it:
1. (Probably easiest) Set up a Comparator that compares your strings by the numeric portion and then run Arrays.sort() on it.
2. Use delims = "[|;]" for your split(). That will create separate entries for the alpha and numeric portions which you can then extract and sort as you see fit.
3. Change the order of your input so that the number part comes first.

Personally, I'd go with 1.

Winston


ok here the code I have so far and the problems it work great for short numbers but I forgot to say the numbers I will be comparing are 13 chars long example "entries.add("7011111111111|jim ");" how would I modify this to do that?


 
justin e wilson
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok I got it to work with BigInteger thank you for your help

public class CustomStringComparator implements Comparator<String>{

@Override
public int compare(String str1, String str2) {
String now1 = str1.substring(0, str1.indexOf("|") - 1);
String now2 = str2.substring(0, str2.indexOf("|") - 1);
BigInteger num3 = new BigInteger(now1);
BigInteger num4 = new BigInteger(now2);
return num3.compareTo(num4);

}
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
justin e wilson wrote:ok I got it to work with BigInteger thank you for your help

Great. I suspect you could have also done it with Long.parseLong(), but BigInteger is pretty much bomb-proof.

I was a little puzzled by the examples you posted though. I was under the impression from your first post that entries were separated by '|' and contained a name and a number, in that order, separated by ';'. For that case, I think I'd have done something like:and then in your main code (taken from your original post):but if what you have works, great.

Winston

BTW: An alternative to loading the array into the list entry by entry is to do:
List<String> entries = Arrays.asList(tokens);
Or indeed, simply sort the array, viz:
Arrays.sort(tokens, EntryComparator);
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!