1

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:

  • I sort the values of 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
  • I sort other ArrayLists by this custom order, but what I do If the values in the 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.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Nelly Junior
  • 556
  • 2
  • 10
  • 30
  • 3
    `Collections.sort` takes an optional `Comparator`. Write one that sorts using the values in the master list. – twentylemon Jun 13 '15 at 17:16
  • 2
    Do you have to create a list for every object you want to compare? If you want to compare another, you have to manually create another `ArrayList`? Why not using **OOP paradigm** and creating an Object with your data, then any time you want to add a new one you just instance a new object and add to your List. Also, when you want to compare them you just use `nameList.get(i).getNameFirstValue()` – Jean Cedron Jun 13 '15 at 17:20
  • See this [related question](http://stackoverflow.com/questions/10213493/can-i-sort-two-lists-in-relation-to-each-other) and [this one](http://stackoverflow.com/questions/17780398/sort-two-arraylists-concurrently), and [this one](http://stackoverflow.com/questions/15400514/syncronized-sorting-between-two-arraylists) – augray Jun 13 '15 at 17:21
  • 3
    Rather than have 6 array lists of attributes, sounds like you should have 1 list of objects that have 6 attributes. Then the problem would be trivial - just sort the list using a comparator for the objects that checks each each attribute in succession – Bohemian Jun 13 '15 at 17:29
  • @twentylemon `Collections#sort` doesn't take a method to change the implementation of `swap`, which is the root of the problem. The comparison is not the issue. – Luiggi Mendoza Jun 13 '15 at 17:30
  • Examples helps me more than suggestions. It doesn't mean that suggestions doesn't help me at all. Thank you all for replies! – Nelly Junior Jun 13 '15 at 17:56

2 Answers2

1

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());
            }
        });        
     }
}
Jean Cedron
  • 702
  • 3
  • 14
0

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.

laune
  • 31,114
  • 3
  • 29
  • 42
  • Thanks for help. I didn't tried your examples, becouse I didn't have time. I personally solved this problem but I am too busy for now to describe the solution. I will try your examples and I will accept your answer if they are better than my sollution, or I will let you know if it worked anyway. Thanks again! – Nelly Junior Jun 24 '15 at 03:01