-1

I have a list of linkedlist.

    List<LinkedList<File1>> lists = 
        Collections.synchronizedList(new ArrayList<LinkedList<File1>>());

each linkedlist contains objects of type File1.

class File1
{
    int dist,nod;       
}

Can anyone help me to sort the elements based on dist.

I thought of using collections.sort() but this cannot be used in this case, so can anyone suggest a better idea?

4

3 回答 3

6

遍历数组列表中的每个链表,然后对其进行排序。但要确保元素按 排序dist,您应该实现 Comparable:

public class File1 implements Comparable<File1>
{
    int dist, nod;

    public int compareTo(File1 f)
    {
        return Integer.compare(dist, f.dist);
    }
}
于 2012-04-11T18:28:28.233 回答
4

您可以使用接受比较器的 Collections.sort() 方法对内部或外部列表进行排序。

公共静态无效排序(列表列表,比较器c)

于 2012-04-11T18:28:07.620 回答
3

将 Collections.sort() 与自定义比较器一起使用,该比较器比较每个对象的 dist 值。

于 2012-04-11T18:28:42.180 回答