• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Mo Ben
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.




 
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Mo Ben
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello guys,

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

https://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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1849
15
Eclipse IDE Spring VI Editor Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awesome!

Good luck!

And you are welcome!
 
I knew that guy would be trouble! Thanks tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic