{"id":1373,"date":"2015-09-23T16:59:44","date_gmt":"2015-09-23T16:59:44","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=1373"},"modified":"2022-02-17T06:24:02","modified_gmt":"2022-02-17T06:24:02","slug":"sort-array","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/memory\/arrays\/sort-array","title":{"rendered":"Sort Array"},"content":{"rendered":"<h4>\nSort Arrays<br \/>\n<\/h4>\n<pre>\r\n<code>\tArray::Sort<\/code><\/pre>\n<p>\nWill sort an array in numerical order, and you can specify multiple arrays and they will bothbe sorted the same (i.e. an array of peoples names sorted according to another array of their ages), eg:\n<\/p>\n<pre>\r\n<code>\tArray::Sort(MyArrayWithSortKeys, MyOtherArray);\t\/\/Sorts the entire Array using the default comparer.<\/code><\/pre>\n<h4>\nSort An Array 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\tArray::Sort(MyMainClassName);\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\tArray::Sort(MyMainClassName);\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sort Arrays Array::Sort Will sort an array in numerical order, and you can specify multiple arrays and they will bothbe sorted the same (i.e. an array of peoples names sorted according to another array of their ages), eg: Array::Sort(MyArrayWithSortKeys, MyOtherArray); \/\/Sorts the entire Array using the default comparer. Sort An Array Of Custom Classes You [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[],"class_list":["post-1373","post","type-post","status-publish","format-standard","hentry","category-arrays"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1373","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=1373"}],"version-history":[{"count":4,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1373\/revisions"}],"predecessor-version":[{"id":1380,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/1373\/revisions\/1380"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=1373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=1373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=1373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}