{"id":234,"date":"2010-08-30T23:17:39","date_gmt":"2010-08-30T13:17:39","guid":{"rendered":"http:\/\/jcrawfor74.wordpress.com\/?p=234"},"modified":"2013-01-24T01:42:38","modified_gmt":"2013-01-23T14:42:38","slug":"generic-list-sort","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2010\/08\/30\/generic-list-sort\/","title":{"rendered":"Generic List.Sort"},"content":{"rendered":"<div id=\"ntsbl-2437017560\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><p>I often want to use a list to sort and every time I have to go searching on how to use the IComparer interface.<\/p>\n<p>Assume I have a Person object with the &#8220;DisplayName&#8221; property that I wish to sort by.<\/p>\n<p>I created the following class:<br \/>\n[csharp]<br \/>\npublic class PersonDisplayNameAscendingComparer : IComparer<br \/>\n{<br \/>\n    #region IComparer Members<\/p>\n<p>    public int Compare(Person x, Person y)<br \/>\n    {<br \/>\n        return x.DisplayName.CompareTo(y.DisplayName);<br \/>\n    }<\/p>\n<p>    #endregion<br \/>\n}<br \/>\n[\/csharp]<\/p>\n<p>And then assuming you have a List called people, you can sort like this;<\/p>\n<p>[csharp]<br \/>\n    PersonDisplayNameAscendingComparer personComparer = new PersonDisplayNameAscendingComparer();<br \/>\n    people.Sort(personComparer);<br \/>\n[\/csharp]<\/p>\n<p>Job done.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I often want to use a list to sort and every time I have to go searching on how to use the IComparer interface. Assume [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-234","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/234","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=234"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/234\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=234"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=234"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=234"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}