Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

sorting a array

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
i've to sort a number list consisting of $,strings,comma's,# etc.I've sorted this list using the
string tokenizer,but the problem is that for printing it back,i need to add those signs($,strings,comma's,#)
back to the sorted list.I think you got my question,if not tell me I'll send u the code.
Can anyone help me plz.
amrit
 
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this:

set up a Map, mapping the number to the formatted string.
Sort the array of numbers (or use a sorted Map...)
Then, using the ordering of the numbers, get the Strings out of the Map.
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks joel,
i never tried maps..isn't there any other alternative??
 
Joel McNary
Bartender
Posts: 1843
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you could try writing your own Comparator that would compare two strings based on their numeric content, ignoring any non-digit characters. Then sort your list using that Comparator.

Personally, though, I think that the map is easier. There's just two methods to worry about: put and get. Take a look at the JavaDoc for java.util.Map and if you have any questions, the Ranch is open around the clock...
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks mr.joel,i tried my hand using maps,help me get further."val" is my original array,
"v" is the array i got after sorting,now i want to add the signs back to v("$",",","dog")before
i display the results.Can u help me how to do that using maps.


String[] val= {"$1.09","234","1,239","1234","64.69","-2.36","4,723.21","dog","2shoe"};
String[] v= {"-2.36","0","1.09","2","64.69","234","1234","1239","4,723.21"};
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think what Joel was getting at regarding using Maps is as follows.

Use a SortedMap.
When you put each element into the map, its key will be the numeric string and its value will be the original string. This will automatically sort the strings in ascending order by the key (which is the numeric value).

That way you don't have to worry about adding all of the special characters back in. You can just access the Map.Entry's value to get the original String.
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi martin,I tried the following,but it sorted by key and showed the output as in array "val".I need the output as in array "v",can u tell me
where i am goin wrong.

public static void main(String ar[])
{

String[] val= {"$1.09","234","1,239","1234","64.69","-2.36","4,723.21","dog","2shoe"};
String[] v= {"-2.36","0","1.09","2","64.69","234","1234","1239","4,723.21"};
String[] k={"0","1","2","3","4","5","6","7","8"};


TreeMap table = new TreeMap();

for(int i=0;i<val.length;i++)
{
table.put(k[i], val[i]);
}


for (Iterator it = table.entrySet().iterator() ; it.hasNext() ; )
{
Map.Entry e = (Map.Entry) it.next(); // get the next key-value pair and store it in e
System.out.println(e.getKey() + " : " + e.getValue());
}
 
Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, well let's assume you have a function that takes in a string and removes all of the special characters. Let's call it:

public String processString(String x)

The problem with the code you posted is, you are setting the keys as arbitrary integers that have no meaning. What I was getting at is something closer to this.



So what should end up happening is you have a Map that looks as follows
key | value
"-2.36" "-2.36"
"0" "dog"
"1.09" "$1.09"
"2" "2shoe"
....

That is what you're looking for, right?
[ December 23, 2004: Message edited by: Martin Mathis ]
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note that letting the SortedMap sort your String keys is not going to sort the same as if you convert them to numbers (float or double) first. Comparison by String value puts "10000000" before "2".

However, this trick can still be used. You could put the Double value into the map as the key or you could keep the Strings and use the Map only to look up the non-munged version while iterating the sorted array.

However, you will still get bitten by duplicate numeric values. For example,Since Maps don't allow duplicate keys, you end up withBoth "dog" and "cat" have a 0 key, and the last one put into the Map wins.

Long story short, your best bet is to create a class that contains both the full String and its numeric value along with an appropriate java.util.Comparable interface implementation. Put those into your array and sort it. This was just covered in this post, though not in any more depth.
[ December 23, 2004: Message edited by: David Harkness ]
 
Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oops, didn't think about duplicate keys
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks,
martin and david,let me work upon ur directions,i'll get back to u with the results.thank u very much.merry christmas...bye.
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends,
with your support everythin is working fine now except the last part i.e when the map contains duplicate values,it behaves as per mr.david's instructions.I tried to write a comparator,but not able to figure out where i'm goin wrong with the comparator,can u plz help me again to reach the end.
 
Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
David was saying that using the SortedMap idea won't work because of the duplicate keys. You will need to create a new class, that implements Comparable.

It should look something like this


After having that, you can simply add them to an ArrayList and call Collections.sort on that ArrayList.

For reference, check out this link

[ December 24, 2004: Message edited by: Martin Mathis ]
[ December 24, 2004: Message edited by: Martin Mathis ]
 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks martin,I know this time i sound like an idiot ..still not able to write the program,even after goin thru tht link.I hope someone can catch the bug and help me..i guess the bug is somewhere b/w the dotted line.help plz......
 
Martin Mathis
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a couple problems in your code, let me point them out for you.
1. You are adding a string to your ArrayList, not an instance of EntryC
2. You can get rid of the entire Entry C array, e
3. Collections.sort doesn't need the e, just send in the ArrayList
4. Your EntryC class lists the numeric_rep of the string as a double, but the processString returns a Double.
5. Because your ArrayList will contain EntryC objects, when you print it out you will need to access the actual string element in the object.

Below is the code with edits I made. I compiled and executed it so I'm pretty sure it works. Let me know if there is anything you're confused about.

 
amrit choong
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PERFECT man..no confusions at all.Thanks for your effort and time and making my life a bit easier.merry christmas.....and happy new year,bye.
 
After some pecan pie, you might want to cleanse your palatte with this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic