How to sort a two-dimensional ArrayList array

I have a two-dimensional ArrayList array that contains double values:

ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>(); 

By analogy with classical arrays, I would like to sort the "cols" of this matrix: I want to take elements with the same index in sub ArrayLists, and then sort them. Like calling Collections.sort () for each column ... By row, I mean that the outer layer and inner layer are columns.

What is the right way to do this? I was thinking of iterating over a matrix to invert it and then sorting each row using Collections.sort ()? but perhaps this is not the best solution, because the matrix is ​​about 400 * 7000.

I cannot use classic arrays since the size of the matrix is ​​unknown.

Thanks for the help.

+5
source share
7 answers

Do something like this:

    final int COLUMN = 5;
    Comparator<ArrayList<Double>> myComparator = new Comparator<ArrayList<Double>>() {
        @Override
        public int compare(ArrayList<Double> o1, ArrayList<Double> o2) {
            return o1.get(COLUMN).compareTo(o2.get(COLUMN));
        }
    };
    Collections.sort(list, myComparator);

Set COLUMN to any column that you sort.

Update:

Yes, that will not work.

I like ahanin's second sentence to create your own list that wraps your original list. You will also have to wrap the object returned by get () so that the wrappedList variable contains the column values, and wrappedList.get (0) also returns a column of values. Then sorting may work. I wonder what minimal methods you should implement for Collections.sort () to work with your list.

It would probably be easier to just take someone else in quicksort and make it work with your list.

: http://www.vogella.de/articles/JavaAlgorithmsQuicksort/article.html

+3

, , , , , , .

ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>(); 

for(int c=0; c<data.get(0).size(); c++) {
    List<Double> col = new ArrayList<Double>();
    for( int r=0; r<data.size(); r++ )
        col.add( data.get(r).get(c) );

    Collections.sort(col);

    for( int r=0; r<col.size(); r++ )
        data.get(r).set( c, col.get(r) );
}

, - , , , , .

+3

: , , Collection, , to Collections.sort(). ArrayList .

+2

. , .

final int column = 3;
ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>(); 

// Convert data into an object array
Object [] temp = data.toArray();

Arrays.sort(temp, new Comparator<Object>() {
    @Override
    public int compare(Object o1, Object o2) {
        // Get inner arrays to be compared by type-casting
        ArrayList<Double> temp1 = (ArrayList<Double>) o1;
        ArrayList<Double> temp2 = (ArrayList<Double>) o2;

        // Then compare those inner arrays' indexes
        return temp1.get(column).compareTo(temp2.get(column));
    }
});

// Clear the old one and add the ordered list into
data.clear();

for(Object o: temp)
{
    data.add((ArrayList<Double>) o);
}
+2

, , SortedMap ArrayList. , .

ArrayList<SortedMap<Double,byte>> data;
0

, Q, "" ( , - ):

    final ArrayList<ArrayList<Double>> data = new ArrayList<ArrayList<Double>>(); 
    //...

    final Integer[] rows = new Integer[data.size()];
    for(int i=0;i<rows.length;i++)
        rows[i]=i;

    int cols = data.get(0).size();
    for(int c=0;c<cols;c++)
    {
        final int colidx = c;
        Arrays.sort(rows,new Comparator<Integer>(){
            @Override
            public int compare(Integer arg0,
                    Integer arg1) {
                return data.get(arg0).get(colidx).compareTo(data.get(arg1).get(colidx));        
            }});    

        for(int i=0;i<rows.length;i++)
            data.get(i).add(colidx+1,data.get(rows[i]).get(colidx));
        for(int i=0;i<rows.length;i++)
            data.get(i).remove(colidx);
    }
0

, . .

int temp[] = new temp[col.length];
int x = 0;
while(x < row.length)
{
    for(int i = 0; i<col.length; i++)
    {
        temp[i] = mat[x][i];
    }
    sort(temp);
    for(int i = 0; i<col.length; i++)
    {
        mat[x][i] = temp[i];
    }
x++;
}

There will be work hours here O(n^2logn), but since you sort only 400 doubles, it won't take long. and since you must each element in the matrix at least once, you cannot go belowO(n^2)

0
source

All Articles