Play Games

Search This Blog

Friday, September 24, 2021

How to compare values of 2 different lists in apex - Salesforce Globe For You

How to compare values of 2 different lists in apex - Salesforce Globe For You

Problem: I have 2 lists ,listA with some values and listB with some values.I need to compare listA and listB and return true if both are same else false.

Sample Code:

List<String> listA = new List<String>{'a','b','d','c'};

listA.sort();

List<String> listB = new List<String>{'a','d','b','c'};

listB.sort();

Boolean bBothListAreSame = false;

if(listA.size() == listB.size()) {

    Boolean bBothSame = true;

    for(integer i = 0; i<listA.size(); i++) {

        if(listA[i] != listB[i]) {

            bBothSame = false;  

        }

    }

    if(bBothSame == true) {

        bBothListAreSame = true;

    } 

        

} else {

    bBothListAreSame = false;

}

system.debug('Both Lists are same:'+bBothListAreSame);

Output:



No comments:

Post a Comment