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
Rahul Sangwan7341Rahul Sangwan7341 

Group by in Wrapper List

Hi I have a wrapper class in which i have a boolean variable. Now i have a list of Wrapper Class which i have to show on VF page, now i want to show the table in such a way that true values comes first and then false. Now i want to prepare a wrapperlist such that all true records come first and then records with false value.
Best Answer chosen by Rahul Sangwan7341
pigginsbpigginsb
One approach would be to add a method, starting with the word "get", such as getWrapperList(), and inside this method you could iterate over your list of objects, putting TRUE instances in one list and FALSE instances in another. Then into a final list variable you can use addAll() to add your true then false lists, returning the final list.

Your VF page would then include "{!wrapperList}" to reference the return value of this method.
public List<Wrapper> getWrapperList() {

    List<Wrapper> trueList = new List<Wrapper>();
    List<Wrapper> falseList = new List<Wrapper>();

    for (Wrapper each : myOriginalWrapperList) {
        if (each.isTrue)
           trueList.add(each);
        else
           falseList.add(each);
    }

    List<Wrapper> finalList = new List<Wrapper>();

    finalList.addAll(trueList);
    finalList.addAll(falseList);

    return finalList;

}

 

All Answers

pigginsbpigginsb
One approach would be to add a method, starting with the word "get", such as getWrapperList(), and inside this method you could iterate over your list of objects, putting TRUE instances in one list and FALSE instances in another. Then into a final list variable you can use addAll() to add your true then false lists, returning the final list.

Your VF page would then include "{!wrapperList}" to reference the return value of this method.
public List<Wrapper> getWrapperList() {

    List<Wrapper> trueList = new List<Wrapper>();
    List<Wrapper> falseList = new List<Wrapper>();

    for (Wrapper each : myOriginalWrapperList) {
        if (each.isTrue)
           trueList.add(each);
        else
           falseList.add(each);
    }

    List<Wrapper> finalList = new List<Wrapper>();

    finalList.addAll(trueList);
    finalList.addAll(falseList);

    return finalList;

}

 
This was selected as the best answer
Rahul Sangwan7341Rahul Sangwan7341
yeah , i also think about same solution but this is like turnaround. here we can do group only by 1 field, if we have more than 1 fields to group.
pigginsbpigginsb
So the previous answer would give you a list where true appears first and false appears second, but within each group there is no specific order. Is this what you are dealing with now?

If you are wrapping sobjects that are the result of a query, then you could use ORDER BY in the query to get the list from the database in a specifi order. When this ordered list from the database is split into the true/false grouping, each of the two groups will still retain the order of the original query result, being separated now by the true/false flag in the wrapper class.

Another option might be as follows... to ask the wrapper instance who it belongs to. Suppose your wrapper included an Integer property, called "category" that would return value based on some evaluation of itself. You could iterate through the wrapper instances, putting each one into a map of lists... Map<Integer, List<Wrapper>>..., then get the keyset into list, sort that list ascending, and use the previous getter method to group them into true/false according to your original question.
Map<Integer, List<Wrapper>> wrapperMap = new Map<Integer, List<Wrapper>>();

// group wrapper instances by their category property (an integer) into the map of lists...
for (Wrapper each : myOriginalWrapperList) {
    List<Wrapper> tempList = wrapperMap.containsKey(each.category)? wrapperMap.get(each.category) : new List<Wrapper>();
    tempList.add(each);
    wrapperMap.put(each.category, tempList);
}

// get list of category values from keyset and sort it ascending
List<Integer> categoryList = new List<Integer>(wrapperMap.keySet());
categoryList.sort();

// iterate through the list of sorted category integers and create a grouped by getting each group from the map
List<Wrapper> categorizedWrapperList = new List<Wrapper>();

for (Integer each : categoryList)
    categorizedWrapperList.addAll(wrapperMap.get(each));

// then use the true/false getter method to return the categorized list grouped into true at the top and false at the bottom.

Is this the sort of grouping within the true/false segments you are interested in obtaining?