{"id":102,"date":"2007-12-07T15:37:00","date_gmt":"2007-12-07T15:37:00","guid":{"rendered":"https:\/\/wdev-blog.azurewebsites.net\/index.php\/2007\/12\/07\/sorting-generic-list-in-c\/"},"modified":"2007-12-07T15:37:00","modified_gmt":"2007-12-07T15:37:00","slug":"sorting-generic-list-in-c","status":"publish","type":"post","link":"http:\/\/panahy.nl\/index.php\/2007\/12\/07\/sorting-generic-list-in-c\/","title":{"rendered":"Sorting Generic List in C#"},"content":{"rendered":"<p>You can sort a generic List in C# using different methods. Here I cover only two options which I found them most handy. The third option is to put the method into a class that implements the IComparer interface.<\/p>\n<p><\/p>\n<p><code><\/p>\n<pre><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> System;<br \/><\/span><span style=\"color:#0000ff;\">using<\/span><span style=\"color:#000000;\"> System.Collections.Generic;<br \/><br \/><\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">class<\/span><span style=\"color:#000000;\"> SortingList<br \/>{<br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> <\/span><span style=\"color:#808080;\">&lt;summary&gt;<\/span><span style=\"color:#008000;\"><br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> SortingList: This snippet code demonstrates the delegate method of sorting lists<br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> I define an Item type having some fields and sort based on a preferred field<br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> To do so I need to implement a method would be called during the sort action for each pair.<br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"><br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> NOTE: You could also create a class implementing IComparer and use the class to pass to the Sort method.<br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"><br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> Asghar Panahy<br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> 07-12-2007<br \/>      <\/span><span style=\"color:#808080;\">\/\/\/<\/span><span style=\"color:#008000;\"> <\/span><span style=\"color:#808080;\">&lt;\/summary&gt;<\/span><span style=\"color:#808080;\"><br \/><\/span><span style=\"color:#000000;\">      <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">static<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">void<\/span><span style=\"color:#000000;\"> Main()<br \/>      {<br \/>            List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">Item<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\"> list <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">Item<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\">();<br \/><br \/>            <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Adding some dummy items<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">        list.Add(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> Item(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">6<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\"> Asghar 1<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">));<br \/>        list.Add(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> Item(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">5<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\"> Asghar 2<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">));<br \/>        list.Add(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> Item(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">3<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\"> Asghar 3<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">));<br \/>        list.Add(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> Item(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">4<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\"> Asghar 4<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">));<br \/>        list.Add(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> Item(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">2<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\"> Asghar 5<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">));<br \/>        list.Add(<\/span><span style=\"color:#0000ff;\">new<\/span><span style=\"color:#000000;\"> Item(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">1<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\"> Asghar 6<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">));<br \/><br \/>            Console.WriteLine(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">Dumping the list:<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">);<br \/>            DumpList(list);<br \/>          <br \/>            <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Sorting it by ID<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">            list.Sort(SortByID);<br \/><br \/>            Console.WriteLine(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">Dumping the list:<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">);<br \/>            DumpList(list);<br \/>          <br \/>            <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Second Option, using anonimous methods<br \/>            <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Sorting it by Birthday<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">            list.Sort(<br \/>                  <\/span><span style=\"color:#0000ff;\">delegate<\/span><span style=\"color:#000000;\"> (Item i1, Item i2)<br \/>                  {<br \/>                        <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\"> i1.Birthday.CompareTo(i2.Birthday);<br \/>                  }<br \/>            );<br \/><br \/>            Console.WriteLine(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">Dumping the list:<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">);<br \/>            DumpList(list);<br \/>          <br \/>            <\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\">  Wait for response before closing it<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\">            Console.ReadLine();   <br \/>    <br \/>      }<br \/>    <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">static<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">int<\/span><span style=\"color:#000000;\"> SortByID(Item s1, Item s2)<br \/>    {<br \/>        <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\"> s1.ID.CompareTo(s2.ID);<br \/>    }<br \/>    <br \/>      <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">static<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">void<\/span><span style=\"color:#000000;\"> DumpList(List<\/span><span style=\"color:#000000;\">&lt;<\/span><span style=\"color:#000000;\">Item<\/span><span style=\"color:#000000;\">&gt;<\/span><span style=\"color:#000000;\"> list)<br \/>      {<br \/>            <\/span><span style=\"color:#0000ff;\">foreach<\/span><span style=\"color:#000000;\">(Item l <\/span><span style=\"color:#0000ff;\">in<\/span><span style=\"color:#000000;\"> list)<br \/>            {<br \/>                  Console.WriteLine(l);<br \/>            }<br \/>      }<br \/>    <br \/>}<br \/><br \/><\/span><span style=\"color:#008000;\">\/\/<\/span><span style=\"color:#008000;\"> Sample Class<\/span><span style=\"color:#008000;\"><br \/><\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">class<\/span><span style=\"color:#000000;\"> Item<br \/>{<br \/>    <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> Item(<\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> id, <\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> name)<br \/>    {<br \/>        <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.ID  <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> id;<br \/>        <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.Name <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> name;<br \/>        <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.Birthday <\/span><span style=\"color:#000000;\">=<\/span><span style=\"color:#000000;\"> DateTime.Now;<br \/>    }<br \/>    <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> Name;<br \/>    <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> ID;<br \/>    <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> DateTime Birthday;<br \/>      <\/span><span style=\"color:#0000ff;\">public<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">override<\/span><span style=\"color:#000000;\"> <\/span><span style=\"color:#0000ff;\">string<\/span><span style=\"color:#000000;\"> ToString()<br \/>    {<br \/>        <\/span><span style=\"color:#0000ff;\">return<\/span><span style=\"color:#000000;\"> String.Format(<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#008080;\">{0} is born on {1}<\/span><span style=\"color:#000000;\">\"<\/span><span style=\"color:#000000;\">, <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.Name, <\/span><span style=\"color:#0000ff;\">this<\/span><span style=\"color:#000000;\">.Birthday.ToLocalTime());<br \/>    }<br \/>}<br \/>}<\/span><\/pre>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>You can sort a generic List in C# using different methods. Here I cover only two options which I found them most handy. The third option is to put the method into a class that implements the IComparer interface. using System;using System.Collections.Generic;public class SortingList{ \/\/\/ &lt;summary&gt; \/\/\/ SortingList: This snippet code demonstrates the delegate method &hellip; <a href=\"http:\/\/panahy.nl\/index.php\/2007\/12\/07\/sorting-generic-list-in-c\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sorting Generic List in C#&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[43,83,82],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"post-thumbnail":false},"uagb_author_info":{"display_name":"Pouya Panahy","author_link":"http:\/\/panahy.nl\/index.php\/author\/pouya\/"},"uagb_comment_info":4,"uagb_excerpt":"You can sort a generic List in C# using different methods. Here I cover only two options which I found them most handy. The third option is to put the method into a class that implements the IComparer interface. using System;using System.Collections.Generic;public class SortingList{ \/\/\/ &lt;summary&gt; \/\/\/ SortingList: This snippet code demonstrates the delegate method&hellip;","_links":{"self":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/102"}],"collection":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":0,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"wp:attachment":[{"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/panahy.nl\/index.php\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}