2011-06-30 9 views
2

ArrayListをDateでソートしたいと思います。 私のArrayListのよう:それは日、月、文字列名を持つ日付と文字列を含むArrayListをどのように並べ替えるのですか?

10 June - name 
15 April - name 
23 July - name 
03 March - name 

。どのように日付で並べ替えるのですか?コレクションクラスで

おかげ

+1

あなたの配列リストのArrayListののですか? "6月10日 - 名前"の1つのオブジェクトですか?またはそれは2/3 /に分割されていますか?オブジェクト? – amit

+0

はい "6月10 -name"のオブジェクトです。 – realuser

答えて

0

リストとコンパレータを渡すことができsort(...)という静的メソッドがあります。

リストからの値を比較し、適切な値を返すコンパレータを作成します。

1

ComparatorおよびComparableをご覧ください。

+0

彼のデータが文字列として表現されているので、私はここで彼にどのように役立つだろうか? – aioobe

0

ここでは例:

List<Date> dates=new ArrayList<Date>(); 
dates.add(new Date(System.currentTimeMillis())); 
dates.add(new Date(System.currentTimeMillis()+231892738)); 
dates.add(new Date(System.currentTimeMillis()-742367634)); 
for(Date d:dates) 
    System.out.println(d); 
Collections.sort(dates); 
for(Date d:dates) 
    System.out.println(d); 

それとも、

Collections.sort(dates,customComparator); 
0

は、あなたのケースのためのCollections.sort方法を試してみてくださいカスタムコンパレータを使用することができます@で指摘したように

Collections.sort(myList, new Comparator<String>(){ 

    @Override 
    public int compare(String s1, String s2) { 
      // perform the comparison here and returns a negative integer, 
      // zero, or a positive integer as the first argument is less than, 
      // equal to, or greater than the second 
    } 
    }); 
6

をエリート紳士、カスタムコンパレータを使用する必要があります。ここでは完全な例です:

import java.text.*; 
import java.util.*; 
public class Test { 
    public static void main(String[] args) { 
     List<String> list = new ArrayList<String>(
       Arrays.asList("10 June - name", 
           "15 April - name", 
           "23 July - name", 
           "03 March - name")); 

     // Print list before sorting. 
     System.out.println(list); 

     // Sort list according to date. 
     Collections.sort(list, new Comparator<String>() { 
      DateFormat df = new SimpleDateFormat("dd MMM"); 
      @Override 
      public int compare(String s1, String s2) { 
       try { 
        Date d1 = df.parse(s1.split("-")[0].trim()); 
        Date d2 = df.parse(s2.split("-")[0].trim()); 
        return d1.compareTo(d2); 
       } catch (ParseException pe) { 
        System.out.println("erro: " + pe); 
        return 0; 
       } 
      } 
     }); 

     // Print list after sorting 
     System.out.println(list); 
    } 
} 

は出力:

[10 June - name, 15 April - name, 23 July - name, 03 March - name] 
[03 March - name, 15 April - name, 10 June - name, 23 July - name] 

良いアイデアしかし、別のクラスに日付/名前のペアをカプセル化することができる。これは、解析する必要性を避けるだろう。これらの目的のために使用する必要があるたびに文字列を返します)。

+0

私はあなたに誰かの宿題をするための+1を与える。 :p –

+0

ハ、そう思う?私は宿題を決して解決しようとしないので、これが宿題ではないことを願っています:P – aioobe

+0

宿題は全部書かれています – Steve

0

この日付文字列をintに変換する関数を作成しますか? すなわち

str2int("name 10 June") => 610 
str2int("name 15 Jan") => 115 

int str2int(String s){ 
    String[] elms = s.split("\\s+"); 
    // ignore first argument which is name 
    int day = Integer.parseInt(elms[1]); 
    int month = month2int(elms[2]); 
    return day + month; 
} 

// month2int("Jan") => 100 
// month2int("Feb") => 200 
// . 
// . 
// month2int("Dec") => 1200 

// you get the idea :-) 

ようなそれらの文字列を比較するコンパレータを使用..

関連する問題