my dog learned polymorphism*
The moose likes Beginning Java and the fly likes How to sort a list of things (string, float)? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to sort a list of things (string, float)?" Watch "How to sort a list of things (string, float)?" New topic
Author

How to sort a list of things (string, float)?

Mo Ben
Greenhorn

Joined: Mar 19, 2010
Posts: 6
Hi guys,

I am new to Android AND to Java. I am trying to sort something like this:

"lamp" = 12
"box" = 3
"chair" =5
....

I would like to sort by the value so that I get the following result:

"box"
"Chair"
"Lamp"

the number a floats. I was thinking of using ArrayList but I do not believe they can handle strings AND floats. I also do not think ArrayList has a SORT method either. I have heard of "MAP" but I am not sure if that make for my need. I just need to sort about 50 elements (string,float) coming back from a Http POST call (JSON)

I am not even sure I am making any sense but I would love some help/pointers.

Thanks a lot.

Mo.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Howdy Ben, welcome to javaranch.

So you have a sort of Key-Value pair which you want to sort. TreeMap would be a good choice for you here. You can store the float as the key and the String as the value.

Since this question isn't related to Android, I'm moving it to the more appropriate forum...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Mo Ben
Greenhorn

Joined: Mar 19, 2010
Posts: 6
Hello Ankit!

Thanks for the warm welcome and answer. I am actually building an Android application that POST to a webservice and got in return a JSON object. After I parse the JSON I wanted to sort the all thing (an array of float) I also want each element of the float array match an array of string. For instance:

floatArray [0]=12
floatArray [1]=3
floatArray [2]=5
....

stringArray[0]="lamp"
stringArray[1]="box"
stringArray[2]="chair"
....

What I am trying to do is sort the floatArray but still keep the stringArray "synchronized with the floatArray (ie. floatArray index 0 will always be refering to a "lamp" string) Obviously if I just sort the floatArray as is I will get:

floatArray [0]=3
floatArray [1]=5
floatArray [2]=12

Which would not work since there is no connection between the floatArray and the stringArray in my example. So you think TreeMap would work for this? I will give it a try. Thank you so much.

Take care.

Mo.




Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

The TreeMap won't be an array, it will match keys to their values.... for example...



The map takes care of matching the values to their keys. So when you want to access them, there's lots of ways in the API to do so.

If you want to do this (easily, but not efficiently) with already made arrays, try this:



Hope that helps!


When you do things right, people won't be sure you've done anything at all.
Mo Ben
Greenhorn

Joined: Mar 19, 2010
Posts: 6
Hello Janeice.

So sorry for the delay. Thank you so much for your answer. I am not stuck on using arrays actually. I will be fine with the key/value type (map) I am wondering now if I can sort this map?


myMap.put(3.6, Chair);
myMap.put(37.78, Coat);
myMap.put(15.34, Stove);

I would like to sort the map in decreasing number (not on the name like chair or stove) The reason I change the numbers from integer to float is that my app uses floats. If this map can be sorted then it will be fantastic. I will look into it and see if it is possible.

In any event thank you both for the great help you provided me. I appreciated it very much.

Mo.



Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

My suggestion is to use a TreeMap. This will automatically sort the Map ASCENDING by KEY.

You can then use methods provided in the API to reverse the order... descendingKeySet(), descendingMap()... I don't really know how those methods work in practice, so you might have to play to get them to work for your needs.

Read through the API and see if it's a good fit for you.
Mo Ben
Greenhorn

Joined: Mar 19, 2010
Posts: 6
Hello guys,

I found the following here. Thanks to Barry Gaunt with this post:

http://www.coderanch.com/t/370496/Java-General/java/Sorting-Hashtable-by-values-retriving

I adopted to my need (mostly changing from integer to float for the value) I will experiment with it shortly but I was wondering if this might work? I do not believe I need to a write a custom comparator right? No sure because this is all new to me! Of course I will need to adapt it to run under Android (is: NO public static void main( String[] args )


Thank you again.

Mo.



Edit - added code tags <JD>
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

Mo, PLEASE use code tags when posting code. It helps to read it.

I'll edit your post this time.... cause you're new
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

Ok, so did you read my post?

Don't use HashMap, use a TreeMap and the sorting will be all set for you.



Edit - Syntax error
Mo Ben
Greenhorn

Joined: Mar 19, 2010
Posts: 6
Hello,

WOW thanks!

1- I am very sorry for the code tagging. I did not know about that.

2- So you are saying that using TreeMap and just by adding element one by one (returns from my JSON parsing) the map will automatically be sorted in decedent order. Ie, iterating over the element of the map I could get something like this:




RESULTS
First element: (37.78,"Coat")
Second element: (15.34,"Stove")
Third element: (3.6,"Chair")

If this is the case then that would be just fantastic!


Thanks again for your great help. it is clear there is a lot I need to learn!

Mo.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

I'm going to have you read this again:

Janeice DelVecchio wrote:My suggestion is to use a TreeMap. This will automatically sort the Map ASCENDING by KEY.

You can then use methods provided in the API to reverse the order... descendingKeySet(), descendingMap()... I don't really know how those methods work in practice, so you might have to play to get them to work for your needs.

Read through the API and see if it's a good fit for you.


Did you READ the API?
Mo Ben
Greenhorn

Joined: Mar 19, 2010
Posts: 6
Oops! I missed that post

It is quite clear now. Thank you very much for your help and especially your patience!

Cheers

Mo.
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1659
    
  11

Awesome!

Good luck!

And you are welcome!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to sort a list of things (string, float)?
 
Similar Threads
ArrayList of Different Datatypes!!
How to check if String() value is numeric
Converting Float[] to float[]
Multiplicity indicator