Pages

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!!!


12 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
  4. This is one of the most important blogs that I have seen, keep it up!
    best design agency

    ReplyDelete
  5. Working in a construction site has a lot of dangers and risks. This is because being a construction worker, you will need to do a lot of dangerous works such as cutting, riveting, sawing, building, and all other sorts of industrial activities. No matter how technically advanced the safety measures and work protocols are, there will always be a possibility that an accident will happen. remodeling contractors San Jose CA

    ReplyDelete
  6. Safe drivers know that they should be aware not only of the dangers of other drivers' negligent behavior, but also of the potential hazards that exist out on the highways. Ongoing construction can present particularly dangers situations, both to drivers and to the workers on site, since there are many hazards present that can potentially cause harm. A construction project can change the familiar nature of roads and can present obstacles that surprise drivers. If the road construction is not properly maintained, repaired, or replaced, it can lead to potential accidents and injuries. concrete driveway lansing mi

    ReplyDelete
  7. “It’s always good to learn tips like you share for blog posting. As I just started posting comments for blog and facing problem of lots of rejections. I think your suggestion would be helpful for me. I will let you know if its work for me too.” Get a free quote

    ReplyDelete
  8. I am incessantly thought about this, thanks for putting up. Kalamazoo Handyman Crew

    ReplyDelete
  9. I went over this site and I believe you have a lot of superb information, saved to bookmarks (:. handyman services lansing mi

    ReplyDelete
  10. Some really wondrous work on behalf of the owner of this site, perfectly great subject material . handyman lansing mi

    ReplyDelete
  11. The Comparable interface provides a convenient way to sort wrapper classes like Lists of user-defined types. It simplifies sorting operations and enhances code readability. Leveraging this interface, programmers can efficiently organize data structures. For further insights, consult with the best assignment writers who can offer valuable guidance on this topic.





    ReplyDelete