如何排序二维的ArrayList [英] How to sort a two-dimension ArrayList

查看:281
本文介绍了如何排序二维的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含双值二维的ArrayList

I have a two-dimension ArrayList that contains double values:

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

在经典的比喻数组,我想这对矩阵的COLS进行排序:我想借此具有子的ArrayList相同的索引中的项目,然后对其进行排序。就像调用Collections.sort()对每一列...
按行我的意思是外层和内层的水平都列。

In analogy with classic arrays , I would like to sort the "cols" of this matrix :I want to take the items having the same index in the sub ArrayLists, and then sort them. Like calling Collections.sort() for every column... By rows I mean the outer level and inner level are columns.

什么是做到这一点的正确方法?
我想到了遍历矩阵反转,然后用Collections.sort排序每一行()?但也许它不是最好的解决办法,因为矩阵是400 * 7000。

What is the proper way to do this? I thought about iterating over the matrix to invert it and then sort each row with Collections.sort() ? but maybe it's not the best solution because the matrix is about 400*7000 .

我不能使用经典阵列因为矩阵的尺寸是未知的。

I can't use classic arrays since the size of the matrix is unknown.

感谢您的帮助。

推荐答案

做这样的事情:

    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 whatever column you're sorting on.

更新:

是的,这是不行的。

我喜欢ahanin的第二个建议,使自己的名单,它封装原来的列表中。你将不得不包裹由get(返回的对象),以及使得可变wrappedList包含列值和wrappedList.get(0)也返回值的列。然后排序可以工作。我不知道最低的方法是,你必须实施Collections.sort()在您的工作列表。

I like ahanin's second suggestion to make your own List which wraps your original List. You would have to wrap the object returned by get() as well so that the variable wrappedList contains the column values and wrappedList.get(0) also returns a column of values. Then the sorting can work. I wonder what the minimum methods are that you have to implement for Collections.sort() to work on your List.

这很可能是最简单的,只是拿别人的快速排序,并使其与您的清单工作。

It would probably be easiest to just take someone else's quicksort and make it work with your list.

下面是一个实现: http://www.vogella.de/articles/JavaAlgorithmsQuicksort /article.html

这篇关于如何排序二维的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆