Sunday, April 8, 2018

Comparable Interface : Way to perform sorting on wrapper class (non-primitive type that is Lists of user-defined types)

Apex provides comparable interface with compareTo method which can be used sort wrapper class.

You can write your comparison logic in compareTo method to sort wrapper.

Below is sample wrapper class, user-defined data type which implements comparable interface.


Below is sample code which you can run in developer console in execute anonymous block.

List<SK_sortUtilityTestWrapper> wrapperList= new List<SK_sortUtilityTestWrapper>();
wrapperList.add(new SK_sortUtilityTestWrapper('D',50.6));
wrapperList.add(new SK_sortUtilityTestWrapper('A',40));
wrapperList.add(new SK_sortUtilityTestWrapper('B',44.2));
system.debug('****wrapperlist before sorting:'+wrapperList);
wrapperList.sort();
system.debug('****wrapperlist after sorting:'+wrapperList);


Logs:


Hope this will help!!!


4 comments:

  1. They’re very good at educating their teams in best practices, and they know when to advise companies like us when we should be doing another way
    web design firm

    ReplyDelete
  2. They are a friendly team whose strong client communication skills have made for a pleasant partnership.
    best UX designers

    ReplyDelete
  3. You guys present there are performing an excellent job.
    top 10 web designing companies

    ReplyDelete