[英]Sorting List of lists/ Arraylists/ Hashmaps with multiple values in Java
[英]Sorting multiple lists by the values of another list in java
我有6个ArrayLists,如下所示:
index: 0 1 2 3 4 5
[12345.12 |123.0 |12.12 |1234.0 |123.12 |123.12 ] <Double>
[2000-01-11 |2000-01-11 |2000-01-11 |2000-01-12 |2000-01-12 |2000-01-11] <String>
[1234 | 1234 | 1234 | 1235 | 1235 | 1234 ] <String>
[4 | 10 | 16 | 24 | 30 | 20 ] <Integer>
[7 | 13 | 19 | 27 | 34 | 25 ] <Integer>
[9 | 15 | 21 | 29 | 35 | 40 ] <Integer>
使用Java, 我想按降序按第一个列表的值对它们进行排序 。 如果第一个列表中的值相等,则以自然顺序按第二个列表中的对应值对两个相等的值进行排序 。 可以通过调用Collection.sort(second_list)以自然顺序对第二个列表中的字符串进行排序。
(示例:由于索引4和5的元素相等,因此我必须以自然顺序对第二个列表中索引4和5的元素进行排序:123.12 = 123.12,但“ 2000-01-12”>“ 2000- 01-11“,因此最后两个索引的顺序为5、4)
排序后的列表应如下所示:
0 3 5 4 1 2
[12345.12 |1234.0 |123.12 |123.12 |123.0 |12.12 ] <Double>
[2000-01-11 |2000-01-12 |2000-01-11 |2000-01-12 |2000-01-11 |2000-01-11] <String>
[1234 | 1235 | 1234 | 1235 | 1234 | 1234 ] <String>
[4 | 24 | 20 | 30 | 10 | 16 ] <Integer>
[7 | 27 | 25 | 34 | 13 | 19 ] <Integer>
[9 | 29 | 40 | 35 | 15 | 21 ] <Integer>
我试图构建一个字符串的ArrayList,其中列表中的每个元素都包含一列中的元素,如我们上面所见(例如:该列为索引0)。 在上面的列中的每个元素之后,我用逗号“,”连接字符串,以便可以在排序后分割字符串(例如:“ 12345.12,2000-01-11,...,9”。因为排序没有不能按预期工作,所以我放弃了这个计划。
我需要一种允许在行中重复值的表。
也许如果第一个列表将是一个Map,其中索引是键,而值是上面ArrayList中的元素,我可以这样做:
Map<Integer, Double>
的值进行排序。 索引-是键,显示的第一个列表中的元素-是值。 所以我得到了索引的顺序:0 3 5 4 1 2 Map<Integer, Double>
中的值重复,该怎么办? 如何在第二个List中按元素的自然顺序对其进行排序? ...我需要一个可以快速排序提到的列表的结构。
使用值名称创建一个项目对象:
public class Item {
private Double value1;
private String value2;
private String value3;
private Integer value4;
private Integer value5;
private Integer value6;
public Double getValue1() {
return value1;
}
public void setValue1(Double value1) {
this.value1 = value1;
}
public String getValue2() {
return value2;
}
public void setValue2(String value2) {
this.value2 = value2;
}
public String getValue3() {
return value3;
}
public void setValue3(String value3) {
this.value3 = value3;
}
public Integer getValue4() {
return value4;
}
public void setValue4(Integer value4) {
this.value4 = value4;
}
public Integer getValue5() {
return value5;
}
public void setValue5(Integer value5) {
this.value5 = value5;
}
public Integer getValue6() {
return value6;
}
public void setValue6(Integer value6) {
this.value6 = value6;
}
}
在其他类中使用此:
public class Test {
ArrayList<Item> items;
public Test(){
items = new ArrayList<>();
this.dataList();
}
public void dataList(){
Item item = new Item();
item.setValue1(12345.12);
item.setValue2("2000-01-11");
// add all your values
items.add(item);
// do the same for all your objects
//Sort your list with a custom Comparator
Collections.sort(items, new Comparator<Item>() {
@Override
public int compare(Item item1, Item item2) {
return item1.getValue1().compareTo(item2.getValue1());
}
});
}
}
这是排序的粗略实现,它使用来自多个相等长度的独立可比较列表的值来创建一个顺序,其中列表表示排序键。
public static void sort( List<List<? extends Comparable>> lol ){
List<Integer> index = new ArrayList<>();
for( int i = 0; i < lol.get(0).size(); ++i ){
index.add( i );
}
Collections.sort( index, new CompInd( lol, index) );
for( int i: index ){
for( int j = 0; j < lol.size(); ++j ){
System.out.print( " " + lol.get(j).get(i) );
}
System.out.println();
}
}
CompInd类为n个列表的索引实现Comparator。
class CompInd implements Comparator<Integer> {
private List<List<? extends Comparable>> comp;
private List<Integer> index;
public CompInd( List<List<? extends Comparable>> comp, List<Integer> index ){
this.comp = comp;
this.index = index;
}
public int compare(Integer ind1, Integer ind2){
for( int i = 0; i < comp.size(); ++i ){
int res =
comp.get(i).get(ind1).compareTo( comp.get(i).get(ind2) );
if( res != 0 ) return res;
}
return 0;
}
}
main方法(根据您的数据)创建三个列表并调用排序。
public static void main( String[] args ){
List<Double> dl1 = new ArrayList<>();
for( double d: new double[]{12345.12, 123.0, 12.12, 1234.0, 123.12, 123.12} ){
dl1.add( d );
}
List<String> sl1 = new ArrayList<>();
for( String s: new String[]{"2000-01-11","2000-01-11","2000-01-11","2000-01-12","2000-01-12","2000-01-11"} ){
sl1.add( s );
}
List<String> sl2 = new ArrayList<>();
for( String s: new String[]{"1234","1234","1234","1235","1235","1234"} ){
sl2.add( s );
}
List<List<? extends Comparable>> lol = new ArrayList<>();
lol.add( dl1 );
lol.add( sl1 );
lol.add( sl2 );
sort( lol );
}
}
当然,这适用于任意数量的并行列表,条件是它们都是实现Comparable的对象的所有列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.