function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mathew Andresen 5Mathew Andresen 5 

split a list into sub groups (maps)

Hi,

I have a list that I want to split up into seperate sub groups.  Basically it's an aggregate result list that I want to split up by account name, and then time period (which I define as a string of year plus quarter).  Here is what I have so far, but I have to think there's a better way.

Thanks,
 
Map<string, Decimal> amountMap = new Map<String, Decimal>();
        string lastname;
        string name;
        string accountId;
        try {
            for (AggListWrapper wrapper:wrapperList) {
                name = wrapper.name;
                accountId = wrapper.accountId;
                decimal amount = wrapper.amount;
                string periodYear = ''+ wrapper.year + '-' + wrapper.period;
                periodYearSet.add(periodYear);
                if (lastName == NULL) { lastName=name;}
                if (lastName != name) {
                    completeRowList.add(new PivotRowWrapper(lastName, accountId, amountMap, NULL));
                    amountMap = new Map<string, Decimal>();
                 }
                amountMap.put(periodYear, amount);
                system.debug('PivotTimeSeries_Class *** account id = ' + accountId + 'end of loop name=' + name + ' lastName=' + lastName + ' amountMap=' + amountMap);
                lastName = name;
             }
        } catch (exception e) {
            system.debug('PivotTimeSeries_Class *** error in the wraperList lopp ' + e);
        }
        periodYearList.addAll(periodYearSet);
        periodYearList.sort();
        completeRowList.add(new PivotRowWrapper(name, accountId, amountMap, NULL));  // line total is null when we first go through

 
Best Answer chosen by Mathew Andresen 5
pconpcon
I don't really know what your wrapper provides or what you're using it for, but here is how I would build a mapping of account names to periodYear to amount
 
Map<String, Map<String, Decimal>> accountMap = new Map<String, Map<String, Decimal>>();

for (AggListWrapper wrapper : wrapperList) {
    String name = wrapper.name;
    Id accountId = wrapper.accountId;
    Decimal amount = wrapper.amount;
    String periodYear = '' + wrapper.year + '-' + wrapper.period;

    if (!accountMap.containsKey(name)) {
        accountMap.put(name, new Map<String, Decimal>());
    }

    accountMap.get(name).put(periodYear, amount);
}

List<String> sortedAccountNames = new List<String>();
sortedAccountNames.addAll(accountMap.keySet());
sortedAccountNames.sort();

This will generate a map that you can travers using the sortedAccountNames list.  If this doesn't help you, please help me understand your code and include the missing class definitions and possibly the usage of this code (Visualforce page?)

All Answers

pconpcon
I don't really know what your wrapper provides or what you're using it for, but here is how I would build a mapping of account names to periodYear to amount
 
Map<String, Map<String, Decimal>> accountMap = new Map<String, Map<String, Decimal>>();

for (AggListWrapper wrapper : wrapperList) {
    String name = wrapper.name;
    Id accountId = wrapper.accountId;
    Decimal amount = wrapper.amount;
    String periodYear = '' + wrapper.year + '-' + wrapper.period;

    if (!accountMap.containsKey(name)) {
        accountMap.put(name, new Map<String, Decimal>());
    }

    accountMap.get(name).put(periodYear, amount);
}

List<String> sortedAccountNames = new List<String>();
sortedAccountNames.addAll(accountMap.keySet());
sortedAccountNames.sort();

This will generate a map that you can travers using the sortedAccountNames list.  If this doesn't help you, please help me understand your code and include the missing class definitions and possibly the usage of this code (Visualforce page?)
This was selected as the best answer
Mathew Andresen 5Mathew Andresen 5
This was very helpful, I figured there was a better way I could be doing this.

Thanks,