简体   繁体   English

用Java在另一个列表后面排序

[英]Sorting a list after another one in Java

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). 因此,我有两个列表,一个包含类型为Sarcina(int Id,String desc)的实体(将其称为ls1),另一个包含整数(将其称为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). 所以,如果我有ls1(Sarcina1,Sarcina2,Sarcina3)和ls2(3,5,4),并且我将l2排序为(5,4,3),那么我想在ls1中将其排序为(Sarcina2,Sarcina3,Sarcina1 )。

Thank you. 谢谢。

You can use Treemap , Which maintains the sorted keys. 您可以使用Treemap ,它维护排序的键。 Here you want to sort your Integers, so add them as keys and add your strings as values of a Treemap . 在这里,您要对Integer进行排序,因此将它们添加为键,并将字符串添加为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. 现在, tm是您想要的。 which has strings sorted according to your integers. 其中包含根据您的整数排序的字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM