{"id":1289,"date":"2015-01-13T18:23:55","date_gmt":"2015-01-13T18:23:55","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=1289"},"modified":"2022-02-17T06:24:03","modified_gmt":"2022-02-17T06:24:03","slug":"sort-list","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/memory\/list\/sort-list","title":{"rendered":"Sort List"},"content":{"rendered":"<h4>\nSort A List<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tMyListName-&gt;Sort();\r\n<\/code><\/pre>\n<h4>\nSort A List Of Custom Classes<br \/>\n<\/h4>\n<p>\nYou need to add a CompareTo method to the class which will be used when&nbsp;Sort is called. &nbsp;It needs to be virtual for C++\/CLI and you also need to add :IComparable to the class definition.\n<\/p>\n<h5>\nExample For An Int Value<br \/>\n<\/h5>\n<p>\n(Uses a &#39;.&#39; for the CompareTo call)\n<\/p>\n<pre>\r\n<code>\r\n\tpublic ref class MyMainClassName : IComparable&lt;MyMainClassName ^&gt;\r\n\t{\r\n\tpublic:\r\n\t\tproperty List&lt;MyClassName^&gt; ^MyClassNames;\r\n\t\t...\r\n\t}\r\n\t\r\n\r\n\tpublic ref class MyListClassName\r\n\t{\r\n\tpublic:\r\n\t\tproperty String ^MyString;\r\n\t\tproperty int MyInt;\r\n\r\n\t\tMyListClassName::MyListClassName(void)\r\n\t\t{\r\n\t\t\t\/\/Strings and arrays will be nullptr until created\r\n\t\t\tMyString = &quot;&quot;;\r\n\t\t}\r\n\r\n\t\t\/\/Add CompareTo to allow sorting of list\r\n\t\tvirtual int MyListClassName::CompareTo(MyListClassName ^other)\r\n\t\t{\r\n\t\t\treturn other-&gt;MyInt.CompareTo(this-&gt;MyInt);\t\t\/\/&lt;&lt;Reverse to reverse sorting order\r\n\t\t}\r\n\t};\r\n\r\n\tMyMainClassName ^MyMainClassName1 = gcnew...\r\n\t\t...\r\n\tMyMainClassName-&gt;Sort();\r\n<\/code><\/pre>\n<h5>\nExample For A String&nbsp;Value<br \/>\n<\/h5>\n<p>\n(Uses &#39;-&gt;&#39; for the CompareTo call)\n<\/p>\n<pre>\r\n<code>\r\n\tpublic ref class MyMainClassName : IComparable&lt;MyMainClassName ^&gt;\r\n\t{\r\n\tpublic:\r\n\t\tproperty List&lt;MyClassName^&gt; ^MyClassNames;\r\n\t\t...\r\n\t}\r\n\t\r\n\r\n\tpublic ref class MyListClassName\r\n\t{\r\n\tpublic:\r\n\t\tproperty String ^MyString;\r\n\t\tproperty int MyInt;\r\n\r\n\t\tMyListClassName::MyListClassName(void)\r\n\t\t{\r\n\t\t\t\/\/Strings and arrays will be nullptr until created\r\n\t\t\tMyString = &quot;&quot;;\r\n\t\t}\r\n\r\n\t\t\/\/Add CompareTo to allow sorting of list\r\n\t\tvirtual int MyListClassName::CompareTo(MyListClassName ^other)\r\n\t\t{\r\n\t\t\treturn other-&gt;MyString-&gt;CompareTo(this-&gt;MyString);\t\t\/\/&lt;&lt;Reverse to reverse sorting order\r\n\t\t}\r\n\t};\r\n\r\n\tMyMainClassName ^MyMainClassName1 = gcnew...\r\n\t\t...\r\n\tMyMainClassName-&gt;Sort();\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sort A List MyListName-&gt;Sort(); Sort A List Of Custom Classes You need to add a CompareTo method to the class which will be used when&nbsp;Sort is called. &nbsp;It needs to be virtual for C++\/CLI and you also need to add :IComparable to the class definition. Example For An Int Value (Uses a &#39;.&#39; for the [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79],"tags":[],"class_list":["post-1289","post","type-post","status-publish","format-standard","hentry","category-list"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=1289"}],"version-history":[{"count":8,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1289\/revisions"}],"predecessor-version":[{"id":1379,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1289\/revisions\/1379"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=1289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=1289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=1289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}