在包装类中返回时按名称按字母顺序排序

问题描述 投票:0回答:1
.
    @AuraEnabled(cacheable=true)
    public static list<ProgramBrandTotalAmount> ReportWithTotalBrandOrder(string programId)
    {
        
        list<Program_Item__c> programBrandList =[SELECT Brand_Name__c 
                                                 FROM Program_Item__c 
                                                 WHERE Program_Item__c.Program__c = :programId];
       
        string uniqueBrandName;
        set<string> uniqueBrandNameSet = new set<string>();
        
        
        for (Program_Item__c brandList: programBrandList)
        {         
            uniqueBrandName = brandList.Brand_Name__c;
            uniqueBrandNameSet.add(uniqueBrandName);
        }
      

        list<ProgramBrandTotalAmount> itemstotalAmountListforAllBrandList = new list<ProgramBrandTotalAmount>();
        decimal totalBrandAmount =0;

        for(string brandName : uniqueBrandNameSet)
        {
            ProgramBrandTotalAmount itemstotalAmountListforBrand = new ProgramBrandTotalAmount();
            for(OrderDestinationItem__c orderdestinationItem :orderDestinationItemsList)
            {
                 
                if(brandName == orderdestinationItem.POS_Item__r.Brand__r.Brand_Name__c)
                {
                    totalBrandAmount += orderdestinationItem.Total_Line_Amount__c;
                    itemstotalAmountListforBrand.ProgramName = orderdestinationItem.OrderDestination__r.ProgramName__c;
                    itemstotalAmountListforBrand.BrandName = brandName;
                    itemstotalAmountListforBrand.ItemTotalAmountForBrand = totalBrandAmount;
                }
               
            }
            if(itemstotalAmountListforBrand.BrandName != null ){
                 
                itemstotalAmountListforAllBrandList.add(itemstotalAmountListforBrand);
            }
           
            totalBrandAmount =0;
         }
         return itemstotalAmountListforAllBrandList;

    }

    public class ProgramBrandTotalAmount
    {
        @AuraEnabled
        public string BrandName {get;set;}
        @AuraEnabled
        public string ProgramName {get;set;}
        @AuraEnabled
        public decimal ItemTotalAmountForBrand {get;set;}
    }

返回 LWC 之前我必须按品牌名称排序

我使用了 sort() 方法,但仍然没有成功

显示应该基于品牌名称,但当涉及到按字母顺序排列的包装时,我不知道如何返回

请帮我解决这个问题

提前致谢

sorting triggers salesforce wrapper apex
1个回答
0
投票

尝试使用

Comparable

这样的事情应该是一个好的开始,你必须决定空值应该是第一个还是最后一个(尽管在你的情况下我猜空值会是更大的问题)

public class ProgramBrandTotalAmount implements Comparable {
    @AuraEnabled public string BrandName {get;set;}
    @AuraEnabled public string ProgramName {get;set;}
    @AuraEnabled public decimal ItemTotalAmountForBrand {get;set;}
    
    public Integer compareTo(Object compareTo) {
        ProgramBrandTotalAmount other = (ProgramBrandTotalAmount) compareTo;
        if (String.isBlank(BrandName)) {
            return String.isBlank(other.BrandName) ? 0 : 1;
        }
        if (String.isBlank(other.BrandName)) {
            return -1;
        }
        return BrandName.compareTo(other.BrandName);
    }
}

一旦收到,请在归还之前致电

itemstotalAmountListforAllBrandList.sort()


现在...以上有点矫枉过正,也许这样做会更简单:

set<string> uniqueBrandNameSet = new set<string>();
for (Program_Item__c brandList: programBrandList)
{         
    uniqueBrandNameSet.add(brandList.Brand_Name__c);
}
List<String> sortedList = new List<String>(uniqueBrandNameSet);
sortedList.sort();

然后循环,

for(string brandName : sortedList)
而不是设置...你的决定。如果将来您需要按品牌和产品进行排序 - 可比较会给您带来更大的灵活性。


最后但并非最不重要的一个真正的优点是,如果可以的话,在 SOQL 中进行排序:

SELECT SUM(Total_Line_Amount__c), POS_Item__r.Brand__r.Brand_Name__c, OrderDestination__r.ProgramName__c
FROM OrderDestinationItem__c
GROUP BY POS_Item__r.Brand__r.Brand_Name__c, OrderDestination__r.ProgramName__c
ORDER BY POS_Item__r.Brand__r.Brand_Name__c,OrderDestination__r.ProgramName__c
© www.soinside.com 2019 - 2024. All rights reserved.