-1

I need some help regarding list sorting. So, I have two lists, one which contains entities of type Sarcina(int Id,String desc) (lets call it ls1), and one which contains integers (lets call it ls2). Both lists have the same size. I am trying to sort both of them at the same time in descending order, interchanging the elements from the same positions in the both lists.

So, if I have ls1(Sarcina1,Sarcina2,Sarcina3) and ls2(3,5,4), and I sort ls2 as (5,4,3), I want to have in ls1 sorted as (Sarcina2,Sarcina3,Sarcina1).

Thank you.

Artyomska
  • 1,309
  • 5
  • 26
  • 53

1 Answers1

3

You can use Treemap, Which maintains the sorted keys. Here you want to sort your Integers, so add them as keys and add your strings as values of a Treemap. It'll automatically sort it. So try someting like following:

TreeMap tm = new TreeMap();

  // Put elements to the map
  //Here "your_integer" is key and "your_string" is value in our Treemap
  tm.put("your_integer","your_string");
  tm.put("your_integer","your_string");

now tm is what you want. which has strings sorted according to your integers.

Kaushal28
  • 5,377
  • 5
  • 41
  • 72
  • you would need two tree maps for this then right....one for sorting by Sarcina...and other for Sorting by the Integer....also you would have to implement Comparator to Sort by the string in Sarcina class – prashant Jan 03 '17 at 16:54
  • @prashant you can refer this link for more info. http://stackoverflow.com/questions/922528/how-to-sort-map-values-by-key-in-java – Kaushal28 Jan 03 '17 at 16:56
  • Tree Map will only sort by keys right...so here since you want to sort both Integer and Sarcina...you would need 2 tree maps..one with key Integer and other with key Sarcina..... – prashant Jan 03 '17 at 17:01
  • @prashant here OP doesn't want to sort both the lists. He wants to sort integers and then wants to arrange the strings according to the sorted integers. Read the example mentioned in question. – Kaushal28 Jan 03 '17 at 17:03
  • Glad to here that :) @Artyomska – Kaushal28 Jan 03 '17 at 17:18