簡體   English   中英

排序多個arraylists的arraylist

[英]Sorting of arraylist of multiple arraylists

我有一個多個arraylists的arraylist像 -

ArrayList<ArrayList<String>> al1=new ArrayList<ArrayList<String>>();

arraylist包含以下元素:

[[Total for all Journals, IOP, IOPscience, , , , , 86, 16, 70, 17, 8, 14, 6, 17, 19, 5], [2D Materials, IOP, IOPscience, 10.1088/issn.2053-1583, 2053-1583, , 2053-1583, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [Acta Physica Sinica (Overseas Edition), IOP, IOPscience, 10.1088/issn.1004-423X, 1004-423X, 1004-423X, , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [Advances in Natural Sciences: Nanoscience and Nanotechnology, IOP, IOPscience, 10.1088/issn.2043-6262, 2043-6262, , 2043-6262, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [Applied Physics Express, IOP, IOPscience, , 1882-0786, 1882-0778, 1882-0786, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]

現在我想對主要的arraylist進行排序。 主要的arraylist包含5個內部arraylists。現在排序必須像每個內部arraylist的第7個元素進行比較,這是整數和內部arraylists按照他們的第7個元素的值排列。

Collections.sort(al1, new Comparator<ArrayList<String>>() {
  @Override public int compare(ArrayList<String> o1, ArrayList<String> o2) {
    return o1.get(7).compareTo(o2.get(7));
  }
});

Java中的索引從0開始。 第7個元素的索引為6 此外,您必須將String轉換為int才能進行正確比較。

試試這個 :

Comparator<ArrayList<String>> cmp = new Comparator<ArrayList<String>>()
{
    public int compare(ArrayList<String> a1, ArrayList<String> a2)
    {
        // TODO check for null value
        return new Integer(a1.get(6)).compareTo(new Integer(a2.get(6));
    }
};

Collections.sort(yourList, cmp);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM