C++ sort array by private variable
<p>I have this class</p>

<pre><code>Class VehicleTwoD
{
private:
doubleArea;
}
</code></pre>

<p>so i want to sort by doubleArea;</p>

<p>at my main.cpp</p>

<p>i got</p>

<pre><code>int main()
{
VehicleTwoD *vehicletwod[100];
sort(vehicletwod[0], vehicletwod[vehicleCounter]);

for (int i=0;i&lt;vehicleCounter;vehicleCounter++)
    {
cout &lt;&lt; "Vehicle " &lt;&lt; i &lt;&lt; end;
cout &lt;&lt; vehicletwod[i]-&gt;toDisplay() &lt;&lt; endl;
    }
}

Below is my operator overload at Vehicle.h


bool VehicleTwoD::operator&lt;(const VehicleTwoD&amp; rhs) const
{
return area &gt; rhs.area;
}
</code></pre>

<p>When i try run the program, i get error on runtime - Segmentation core dump fault.</p>

<p>Assuming my Vehicle Counter is not wrong and my Vehicletwod got at least 2 object when i try run toDisplay, whats actually went wrong..</p>

<p><strong>Update:</strong> </p>

<p>I just did this thanks to the valuable feedback i receive from the experts here.</p>

<p>at main.cpp</p>

<pre><code>int main()
{
VehicleTwoD *vehicletwod[100];

for(int i=0;i&lt;100;i++)
{
vehicletwod[i] = new VehicleTWoD();
}

//some computation then i go to sort..

sort(&amp;vehicletwod[0],&amp;vehicletwod[vehicleCounter]);

for(int i=0;i&lt;vehicleCounter;i++)
{
cout &lt;&lt; vehicletwod-&gt;toDisplay() &lt;&lt; endl;
}

}
</code></pre>

<p>The output remain the same, nothing sorted.. i not sure why..</p>

<p>this is the boolean operator i did at the VehicleTwoD.h</p>

<p>VehicleTwoD.h</p>

<pre><code>public:
bool operator&lt;(const VehicleTwoD&amp;) const;
</code></pre>

<p>VehicleTwoD.cpp</p>

<pre><code>bool VehicleTwoD::operator&lt;(const VehicleTwoD&amp; rhs) const
{
return area &gt; rhs.area;
}
</code></pre>

<p>Nothing is sorted.. I would hope at least it sort in either descending or ascending..</p>

<p>I was wonder if theres a way i can allocate my VehicleTwoD array into a vector call sortVector, because after sort in ascending, i wanna sort in descending also.</p>

<p>so a reverse function in vector will be good to solve it.</p>

<p><strong>Any suggestion ? Thanks all the helpful and kind experts here!!</strong></p>
