I have 6 ArrayLists as shown below:
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>
Using Java, I want to sort them by the values of the first list in descending order . If the values in the first list are equals, then sort the two equal values by the corresponding values in the second list in natural order . The strings in the second list can be sorted in natural order by calling Collection.sort(second_list).
(Example: because the elements at index 4 and 5 are equals, I have to sort them by the elements at index 4 and 5 in the second list in natural order: 123.12 = 123.12 but "2000-01-12" > "2000-01-11", so the order of the last two indexes will be 5, 4)
The sorted lists should look like these:
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>
I have tried to build a ArrayList of strings where each element in list contain elements from one column as we can see above (ex: the column as index 0). After each element in the column above I concatenate the string with a comma "," so that I can split the string after sorting (ex: "12345.12,2000-01-11,...,9". Becouse the sort didn't work as I expected, I have abandoned this plan.
I need a kind of a table that allows duplicate values in rows.
Maybe if the first List will be a Map where indexes are keys and values are the elements in the ArrayLists above I can do this:
Map<Integer, Double>
by values. Indexes - are keys, elements in first list presented - are values. So I obtain the order of indexes: 0 3 5 4 1 2 Map<Integer, Double>
are duplicates? How do I sort them by a natural order of elements in the second List? ... I need a structure that sorts fast the Lists mentioned.
Create an item object with your values names:
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;
}
}
And in other class use this:
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());
}
});
}
}
This is a sketchy implementation of a sort that uses values from several independent Comparable lists of equal length to create an order where the Lists represent the sort keys.
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();
}
}
Class CompInd implements the Comparator for indices of the n Lists.
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;
}
}
The main method creates three Lists (from your data) and calls the sort.
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 );
}
}
This works for any number of parallel Lists, provided they are all Lists of objects implementing Comparable, of course.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.