LoginSignup
5
1

More than 5 years have passed since last update.

[apex:コード]MAP の中の日付でソートをする

Last updated at Posted at 2015-04-07

Sort は List にはあるけど、MAPでするとこんな感じになる。

mapsort.java
    map<String,Date>  ADMapInfo = new  map<String,Date>();

    ADMapInfo.put('A10001', Date.newInstance(2014, 10, 01));
    ADMapInfo.put('A10002', Date.newInstance(2014, 01, 19));
    ADMapInfo.put('A10003', Date.newInstance(2014, 09, 23));
    ADMapInfo.put('A10004', Date.newInstance(2013, 12, 07));

    //IDを保存
    map<String,String> aMap = new Map<String,String>();
    //IDのリスト
    List<String> aList = new List<String>();
    //データのリスト
    List<Date> values = new List<Date>();

    //IDのリスト作成
    aList.addAll(ADMapInfo.keySet());

    //IDのリスト回しながら、データのリスト作成する
    //また、データ取出し用のデータを作る。
    for(String str:aList)
    {
        values.add(ADMapInfo.get(str));
        aMap.put(str+ADMapInfo.get(str).format(),str);
    }
    values.sort();
    for(Date s: values)
    {
        for(String str:aList)
        {
            if(aMap.get(str+s.format()) <> null){
                system.debug(str+ '>>' + s.format());
            }
        }
    }
5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1