{"id":3033,"date":"2012-08-07T00:08:53","date_gmt":"2012-08-06T18:38:53","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=3033"},"modified":"2012-08-07T00:08:53","modified_gmt":"2012-08-06T18:38:53","slug":"how-to-sort-wrapper-class-collection-in-apex","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/how-to-sort-wrapper-class-collection-in-apex\/","title":{"rendered":"How to Sort Wrapper class Collection in Apex"},"content":{"rendered":"<p style=\"text-align: justify;\">There is inbuilt functionality in Apex to sort the primitive datatypes supported by force.com. I am sure maximum of developers must have come across the situations where they need to sort custom datatype built by them i.e. custom class or I would say wrapper class in terms of force.com.<\/p>\n<p style=\"text-align: justify;\">But think, How force.com will come to know that how to sort class? In case of integer, date or double they already know. But how they are going to know that what is going to be content in your class? Before <strong>Summer&#8217;12<\/strong> there was no direct way. However thanks to Salesforce to introduce the interface Comparable in Summer&#8217;12 release. And&#8230; there is smile on the face of Java developers, because they already know what it is.<\/p>\n<p style=\"text-align: justify;\">So let&#8217;s start with &#8220;<strong>What is the use of interface Comparable?<\/strong>&#8220;\u009d I would say, this is the way to tell force.com that how we are going to compare the custom Objects (Wrapper class).<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Comparable Interface:<\/strong><\/span><\/p>\n<p>To implement the Comparable Interface, you must declare global class with the implements keyword as follow:<!--more--><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nglobal class Book implements Comparable\n<\/pre>\n<p>Now, your class must implement the method &#8220;<strong>compareTo<\/strong>&#8220;\u009d as follow:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nglobal  Integer compareTo(Object objToCompare)\n{\n\t\/\/Your code to implement logic\n}\n<\/pre>\n<p><span style=\"text-decoration: underline;\"><strong>compareTo() method:<\/strong><\/span><\/p>\n<p>The compareTo() method must return following integer value<\/p>\n<ol>\n<li>0 if both object is equal<\/li>\n<li>&gt;0 if the instance object is greater than &#8220;objToCompare&#8221;\u009d<\/li>\n<li>&lt;0 if the instance object is smaller than &#8220;objToCompare&#8221;\u009d<\/li>\n<\/ol>\n<p><span style=\"text-decoration: underline;\"><strong>Sample code<\/strong><\/span><br \/>\nLet&#8217;s take the example of class &#8220;Book&#8221;\u009d, which have following data members,<\/p>\n<ul>\n<li>BookTitle &#8211; String<\/li>\n<li>Author &#8211; String<\/li>\n<li>TotalPages &#8211; Integer<\/li>\n<li>Price &#8211; Double<\/li>\n<\/ul>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nglobal class Book implements Comparable {\n\n\tpublic String BookTitle ;\n\tpublic String Author ;\n\tpublic Integer TotalPages ;\n\tpublic Double Price ;\n\tpublic Date publishingDate;\n\n\tpublic enum SORT_BY {\n\t\tByTitle,ByPage\n\t\t}\n\n\t\/\/Variable to decide the member on which sorting should be performed\n\tpublic static SORT_BY sortBy = SORT_BY.ByTitle;\n\n\tpublic Book(String bt, String a, Integer tp, Double p, Date pd)\n\t{\n\t\tBookTitle = bt;\n\t\tAuthor = a;\n\t\tTotalPages = tp;\n\t\tPrice = p;\n\t\tpublishingDate = pd;\n\t}\n\n\tglobal Integer compareTo(Object objToCompare) {\n\t\t\/\/Sort by BookName Alphabetically\n\t\tif(sortBy == SORT_BY.ByTitle)\n\t\t{\n\t\t\treturn BookTitle.compareTo(((Book)objToCompare).BookTitle);\n\t\t}\n\t\telse \/\/Sort by Book price\n\t\t{\n\t\t\treturn Integer.valueOf(Price - ((Book)objToCompare).Price);\n\t\t}\n\t}\n}\n<\/pre>\n<p>As you can see in above code, we are sorting on the basis of either BookTitle or Price. The logic can be anything depending on business requirement.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Running the sample code in Developer Console:<\/strong><\/span><br \/>\nRun below code in &#8220;Developer Console&#8221;\u009d from browser or &#8220;Execute Anonymous&#8221;\u009d from Eclipse.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nBook&#x5B;] books = new Book&#x5B;]{\n\tnew Book('Salesforce Handbook','Jeff Douglas',360,35,Date.newInstance(2011, 03, 20)),\n\tnew Book('Let Us C','Yashavant P. Kanetkar',593,58,Date.newInstance(2008, 03, 21)),\n\tnew Book('Head First Design Patterns ','Elisabeth Freeman',678,28,Date.newInstance(2004, 11,01))\n};\n\nBook.sortBy = Book.SORT_BY.ByTitle;\nbooks.sort();\nSystem.debug(books);\n\nBook.sortBy = Book.SORT_BY.ByPrice;\nbooks.sort();\nSystem.debug(books);\n<\/pre>\n<p>When you will see the output, first time it sorted by BookTitle and second time it is sorted according Price.<br \/>\nI hope this post will be helpful to many salesforce developers.<br \/>\nHappy coding!!!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There is inbuilt functionality in Apex to sort the primitive datatypes supported by force.com. I am sure maximum of developers must have come across the situations where they need to sort custom datatype built by them i.e. custom class or I would say wrapper class in terms of force.com. But think, How force.com will come [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[20,9],"tags":[337,331],"class_list":["post-3033","post","type-post","status-publish","format-standard","hentry","category-apex","category-salesforce","tag-apex","tag-salesforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":4721,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/list-of-ide-available-for-salesforce-coding\/","url_meta":{"origin":3033,"position":0},"title":"List of IDE available for Salesforce coding","author":"Jitendra","date":"July 17, 2015","format":false,"excerpt":"List of all major IDE like Eclipse, Welkins, Cloud9 etc to code Apex, Visualforce, Trigger and lightning components in Salesforce","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Cloud9 IDE","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/07\/Salesforce-Cloud9-IDE.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":1241,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-trigger-and-test-cases-6-of-6\/","url_meta":{"origin":3033,"position":1},"title":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and Test cases \u2013 6 of 6","author":"Jitendra","date":"October 11, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and test cases \u2013 6 of 6 tutorials series","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Creating Trigger in Salesforce using force.com IDE","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Creating-Trigger-in-Salesforce-using-force.com-IDE.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":968,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/install-force-com-ide-in-eclipse-salesforce-com\/","url_meta":{"origin":3033,"position":2},"title":"Install or Update Force.com IDE in Eclipse &#8211; Salesforce.com","author":"Jitendra","date":"September 4, 2010","format":false,"excerpt":"Install the Salesforce IDE and Set up the Salesforce Project in Eclipse.","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Eclipse Kepler","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/09\/Eclipse-Kepler.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":4240,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/how-to-become-salesforce-admin-and-developer-trailhead-is-good-answer\/","url_meta":{"origin":3033,"position":3},"title":"How to start learning Salesforce &#8211;  Trailhead is good answer","author":"Jitendra","date":"February 9, 2015","format":false,"excerpt":"In last 3-4\u00a0years, Salesforce is well known cloud computing platform between\u00a0IT professionals and fresh college graduates. Dreamforce, well known for largest conference on planet breaking its own record every year. Salesforce is also\u00a0top Innovative company in world as per forbes. Change is inevitable - well adapted by Salesforce.\u00a0Unlike traditional languages,\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Trailhead - Data Security","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/02\/Trailhead-Data-Security.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/02\/Trailhead-Data-Security.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/02\/Trailhead-Data-Security.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2015\/02\/Trailhead-Data-Security.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":2998,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex-based-sharing-in-salesforce\/","url_meta":{"origin":3033,"position":4},"title":"Apex based record sharing in Salesforce","author":"Jitendra","date":"July 27, 2012","format":false,"excerpt":"Working with Apex based sharing in Salesforce","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Salesforce Apex Sharing Reason","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/07\/Salesforce-Apex-Sharing-Reason.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":2835,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-apex-code-talk-important-questions-and-answers\/","url_meta":{"origin":3033,"position":5},"title":"Salesforce Apex Code talk &#8211; Important Questions and Answers","author":"Jitendra","date":"May 3, 2012","format":false,"excerpt":"Salesforce Apex Code talk on - 24-April-2012 , Important Interview Questions","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3033","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=3033"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/3033\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=3033"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=3033"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=3033"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}