English 中文(简体)
根据其在 java 中的索引对矩阵列表的矩阵列表进行排序
原标题:Sorting an arraylist of arraylist by its index in java

我有一个阵列列表的阵列列表, 每个内部阵列列表包含 2 值, 第一个是整数, 第二个是一个字符串, 基本上是一个字符串, 它看起来就像 : { 5, 某些文本}, { 12, 一些文本}, { 12, 一些更多的文本}, { 3, 甚至更多的文本} 等, 我想做的是用这个列表来排序它, 以最大整数向最小的递减顺序排序它, 所以上一个示例会看起来像 : { 12, 一些更多的文本}, { 5, 某些文本}, { 3, 甚至更多的文本}, 任何帮助都会大大提前感谢 。

最佳回答

your data structure sounds like it is a Map actually. Maybe you should look into data structures, and collection interfaces and classes in particular...

如果您仍然认为自己拥有的是一个列表, 那么您应该用正确的比较或可比较来进行收藏. sort 操作 。

如果清单是正确的,则您的数据结构有一个解决方案;

import java.util.ArrayList;
import java.util.Collections;

public class InnerObject implements Comparable<InnerObject> {
    Integer index;
    String  name;

    public InnerObject(Integer index, String name) {
        this.index = index;
        this.name = name;
    }

    @Override
    public int compareTo(InnerObject other) {
        return index.compareTo(other.index);
    }

    @Override
    public String toString() {
        return "{" + index + "," + name + "}";
    }

    public static void main(String[] args) {
        ArrayList<InnerObject> list = new ArrayList<InnerObject>();
        list.add(new InnerObject(666, "devil"));
        list.add(new InnerObject(1, "one"));
        list.add(new InnerObject(10, "ten"));

        System.out.println("list before order: " + list);

        Collections.sort(list);

        System.out.println("list after order: " + list);

    }   
}
问题回答

您的内部列表中如果包含一个具有两个属性的对象,将会更好。此对象不能用于排序。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签