<p>I am trying to build a function in C/C++ to sort an array and replace each value with its "score" or rank.  It takes in a double pointer array to an array of ints, and sorts the double pointers based on the dereferenced value of the integers.  I have tried quite a few times to make it work, but can't get it down.  Once again, it must sort the double pointers based on the values they point to.  This is what I have:</p>

<pre><code>void SortArray( int ** pArray, int ArrayLength )
{
  int i, j, flag = 1;     // set flag to 1 to begin initial pass
  int * temp;             // holding variable orig with no *
  for(i = 1; (i &lt;= ArrayLength) &amp;&amp; flag; i++)
  {
    flag = 0;
    for (j = 0; j &lt; (ArrayLength -1); j++)
    {
        if (*pArray[j+1] &gt; *pArray[j])    // ascending order simply changes to &lt;
        { 
            temp = &amp;pArray[j];            // swap elements
            pArray[j] = &amp;pArray[j+1];
            pArray[j+1] = &amp;temp;
            flag = 1;                     // indicates that a swap occurred.
        }
    }
  }
}
</code></pre>

[{'answer_id': 17312, 'author': 'OJ.', 'author_id': 611, 'author_profile': 'https://Stackoverflow.com/users/611', 'pm_score': 4, 'selected': True, 'text': '<p>You\'re close. You\'re referencing the address of the array items when you swap, which isn\'t necessary. The items in the array are pointers, and that\'s what needs to be swapped.</p>\n\n<p>See below:</p>\n\n<pre><code>void SortArray( int ** pArray, int ArrayLength )\n{\n    int i, j, flag = 1;    // set flag to 1 to begin initial pass\n    int * temp;             // holding variable orig with no *\n    for(i = ArrayLength - 1; i &gt; 0 &amp;&amp; flag; i--)\n    {\n        flag = 0;\n        for (j = 0; j &lt; i; j++)\n        {\n            if (*pArray[j] &gt; *pArray[j+1])      // ascending order simply changes to &lt;\n            { \n                temp = pArray[j];             // swap elements\n                pArray[j] = pArray[j+1];\n                pArray[j+1] = temp;\n                flag = 1;               // indicates that a swap occurred.\n            }\n        }\n    }\n}\n</code></pre>\n\n<p>Also, check out <a href="http://rant.blackapache.net/2008/08/14/sorting-algorithms-the-bubble-sort/" rel="nofollow noreferrer">this lovely blog post on Bubble Sorting</a> in case you\'re interested (sorry, shameless plug :)). Hope that helps you with your homework ;)</p>\n\n<hr>\n\n<p>Edit: Note the subtle "optimisation" where you count back from the array length and only increment up until \'i\' in the inner loop. This saves you from needlessly reparsing items that have already been sorted.</p>\n'}, {'answer_id': 17332, 'author': 'Brian Ensink', 'author_id': 1254, 'author_profile': 'https://Stackoverflow.com/users/1254', 'pm_score': 2, 'selected': False, 'text': '<blockquote>\n  <p>Heh, this isnt homework. </p>\n</blockquote>\n\n<p>If thats the case then consider using the STL to manage arrays and sort.  Its easier to develop and maintain and the std::sort algorithm is asymptotically faster than bubble sort.</p>\n'}, {'answer_id': 17335, 'author': 'Adam', 'author_id': 1366, 'author_profile': 'https://Stackoverflow.com/users/1366', 'pm_score': 2, 'selected': False, 'text': '<p>You should consider using <code>std::swap()</code> to do your swapping. If you do, call it as such:</p>\n\n<pre><code>swap( obj1, obj2 );\n</code></pre>\n\n<p>rather than:</p>\n\n<pre><code>std::swap( obj1, obj2 );\n</code></pre>\n\n<p>As the first calling semantic will allow the proper namespace lookup to find the correct overload if one exists. Be sure to have either:</p>\n\n<pre><code>using namespace std;\n</code></pre>\n\n<p>or:</p>\n\n<pre><code>using std::swap;\n</code></pre>\n\n<p>somewhere.</p>\n'}, {'answer_id': 18042, 'author': 'Brian Ensink', 'author_id': 1254, 'author_profile': 'https://Stackoverflow.com/users/1254', 'pm_score': 1, 'selected': False, 'text': "<blockquote>\n  <p>Hmm, I don't have much experience with the STL. Could you give an example?</p>\n</blockquote>\n\n<p>This program creates a vector of ints, sorts it, and displays the results.</p>\n\n<pre><code>#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n#include &lt;iostream&gt;\nusing namespace std;\n\nint main()\n{\n    vector&lt;int&gt;; vec;\n    vec.push_back(7);\n    vec.push_back(5);\n    vec.push_back(13);\n    sort(vec.begin(), vec.end());\n\n    for (vector&lt;int&gt;::size_type i = 0; i &lt; vec.size(); ++i)\n    {\n        cout &lt;&lt; vec[i] &lt;&lt; endl;\n    }\n}\n</code></pre>\n"}, {'answer_id': 112530, 'author': 'paercebal', 'author_id': 14089, 'author_profile': 'https://Stackoverflow.com/users/14089', 'pm_score': 0, 'selected': False, 'text': '<p>To complete Brian Ensink\'s post, you\'ll find the STL full of surprises. For example, the std::sort algorithm:</p>\n\n<pre><code>#include &lt;iostream&gt;\n#include &lt;vector&gt;\n#include &lt;algorithm&gt;\n\nvoid printArray(const std::vector&lt;int *&gt; &amp; p_aInt)\n{\n   for(std::vector&lt;int *&gt;::size_type i = 0, iMax = p_aInt.size(); i &lt; iMax; ++i)\n   {\n      std::cout &lt;&lt; "i[" &lt;&lt; static_cast&lt;int&gt;(i) &lt;&lt; "] = " &lt;&lt; reinterpret_cast&lt;unsigned     int&gt;(p_aInt[i]) &lt;&lt; std::endl ;\n   }\n\n   std::cout &lt;&lt; std::endl ;\n}\n\n\nint main(int argc, char **argv)\n{\n   int a = 1 ;\n   int b = 2 ;\n   int c = 3 ;\n   int d = 4 ;\n   int e = 5 ;\n\n   std::vector&lt;int *&gt; aInt ;\n\n   // We fill the vector with variables in an unordered way\n   aInt.push_back(&amp;c) ;\n   aInt.push_back(&amp;b) ;\n   aInt.push_back(&amp;e) ;\n   aInt.push_back(&amp;d) ;\n   aInt.push_back(&amp;a) ;\n\n   printArray(aInt) ; // We see the addresses are NOT ordered\n   std::sort(aInt.begin(), aInt.end()) ; // DO THE SORTING\n   printArray(aInt) ; // We see the addresses are ORDERED\n\n   return EXIT_SUCCESS;\n}\n</code></pre>\n\n<p>The first printing of the array will show unordered addresses. The second, after the sort, will show ordered adresses. On my compiler, we have:</p>\n\n<pre><code>i[0] = 3216087168\ni[1] = 3216087172\ni[2] = 3216087160\ni[3] = 3216087164\ni[4] = 3216087176\n\ni[0] = 3216087160\ni[1] = 3216087164\ni[2] = 3216087168\ni[3] = 3216087172\ni[4] = 3216087176\n</code></pre>\n\n<p>Give STL\'s &lt;algorithm&gt; header a look <a href="http://www.cplusplus.com/reference/algorithm/" rel="nofollow noreferrer">http://www.cplusplus.com/reference/algorithm/</a>\nYou\'ll find a lot of utilities. Note that you have other implementation of containers that could suit you better (std::list? std::map?).</p>\n'}]
['https://Stackoverflow.com/questions/17299', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/522/']