Android 对多维对象或 int 数组进行排序

发布于 2024-12-04 23:27:31 字数 474 浏览 0 评论 0原文

有没有办法对多维对象进行排序?

Object[][] datetable= new Object[2][2];

    datetable[0][0] = 2;
    datetable[0][1] = "Name2";
    datetable[1][0] = 1;
    datetable[1][1] = "Name1";

如果没有,我将如何对多维 int 数组进行排序?

int[][] numtable= new int[2][2];

    numtable[0][0] = 2;
    numtable[0][1] = 20;
    numtable[1][0] = 1;
    numtable[1][1] = 10;

我想使用第一组数字(numbtable[x] 或 datetable[x])对其进行排序 我可以得到一个多维字符串来排序没有问题,但这些我有麻烦。

Is there a way to sort a multidimensional Object?

Object[][] datetable= new Object[2][2];

    datetable[0][0] = 2;
    datetable[0][1] = "Name2";
    datetable[1][0] = 1;
    datetable[1][1] = "Name1";

If not, how would I sort a multidimensional int array?

int[][] numtable= new int[2][2];

    numtable[0][0] = 2;
    numtable[0][1] = 20;
    numtable[1][0] = 1;
    numtable[1][1] = 10;

I would like to sort it using the first set of numbers (numbtable[x] or datetable[x])
I can get a multidimensional string to sort no problem, but these I have trouble with.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肩上的翅膀 2024-12-11 23:27:31

对 int 数组进行排序是通过使用 Integer 对象并使用比较器来完成的,类似于 hotveryspicy 的答案,如下所示:

Integer[][] numtable = new Integer[2][2];
numtable[0][0] = 2;
numtable[0][1] = 20;
numtable[1][0] = 1;
numtable[1][1] = 10;

Arrays.sort(numtable, new Comparator<Integer[]>() {
    @Override
    public int compare(Integer[] int1, Integer[] int2) {
        return int1[0].compareTo(int2[0]);
    }

});

但是,从您的评论来看,您似乎表明您将有一个 5x12 的对象数组,其中散布着字符串和整数,但不彼此不相关(排序失去了项目之间的任何联系,这就是我这样认为的原因),您似乎将其合并到一个多维数组而不是单独的列表中(这就是我可能会去的方式)。

如果您确实转换为整数/字符串列表,则使用以下命令对它们进行排序,在本例中,我显示 int 版本:

List<Integer> myList = new ArrayList<Integer>();
myList.add(2);
myList.add(1);
Collections.sort(myList);
System.out.println("myList is: " + myList);

Sorting arrays of int's is done through using Integer objects and using a comparator, similar to hotveryspicy's answer, as here:

Integer[][] numtable = new Integer[2][2];
numtable[0][0] = 2;
numtable[0][1] = 20;
numtable[1][0] = 1;
numtable[1][1] = 10;

Arrays.sort(numtable, new Comparator<Integer[]>() {
    @Override
    public int compare(Integer[] int1, Integer[] int2) {
        return int1[0].compareTo(int2[0]);
    }

});

However, from your comments, you seem to indicate you'll have a 5x12 array of objects interspersed with strings and ints which don't relate to each other (the sorting loses any connection between your items, which is what makes me think this), which you seem to be merging into one multidimensional array instead of separate lists (which is how i'd probably go).

if you do convert to a List of ints/strings, then use the following to sort them, in this case, i show the int version:

List<Integer> myList = new ArrayList<Integer>();
myList.add(2);
myList.add(1);
Collections.sort(myList);
System.out.println("myList is: " + myList);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文