Link to home
Start Free TrialLog in
Avatar of Roger Alcindor
Roger Alcindor

asked on

list sort problem

I am using Embarcadero C++ builder XE10.1 Berlin update 2.I am having trouble sorting a  TList .
To illustrate the problem, I have coded a simple example below which generates 30 random integers and puts them into a class which is added into a list.
The original list content is displayed in Memo1. the list is sorted using a compare function and the sorted list is displayed in Memo2.
The resultant display shows that the sort operation fails. I have inspected the comarisons by setting a breakpoint in the compare function and can see no failure there.
What am I doing wrong ?

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop
using namespace std;
#include <list>
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;

class MyClass
{
	public:
	int n;
	MyClass(int a){n=a;};
	~MyClass(){};
};

int SortOnInt(void *Item1, void *Item2)
{
	MyClass *a = reinterpret_cast<MyClass *>(Item1);
	MyClass *b = reinterpret_cast<MyClass *>(Item2);
	int rtn = 0;

	if(a!=NULL && b!=NULL)
	{
		if(a->n  > b->n)
			rtn = 1;
		else
		if(a->n  < b->n)
			rtn = -1;
	}
	return rtn;
}
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
	list<MyClass *> myclasslist;
	list<MyClass *>::iterator k;
	int i,j;

	Memo1->Clear();
	Memo2->Clear();
	for(i=0;i<30;i++)
	{
		MyClass *mc;
		j = rand()%100;
		mc = new MyClass(j);
		Memo1->Lines->Add(mc->n);
		myclasslist.push_back(mc); // random number in the range 0 to 99
	}
	// now sort he list
	myclasslist.sort(SortOnInt);

	for(k=myclasslist.begin();k!=myclasslist.end();k++)
	{
		Memo2->Lines->Add((*k)->n);
	}
}
//---------------------------------------------------------------------------

Open in new window

User generated image
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
when using std::list::sort your class simply should provide a less operator (as suggested by evilrix).

class MyClass
{
	public:
	int n;
	MyClass(int a){n=a;};
	~MyClass(){};
        bool operator< (MyClass & other) const
        {
              return n < other.n;
        }

};

Open in new window


with that the sorting will be done by

// now sort the list after fill
myclasslist.sort();

Open in new window


Sara
Avatar of Roger Alcindor
Roger Alcindor

ASKER

Thanks for your comments.
In the project application, I will be sorting the list on various other members of the class depending upon circumstances so in this case, the use of a single compare function in myclass would require my adding a member to myclass that defines which member the sort should be made on.

In an old Borland C++ project of mine (BCB 5) ,the STL List Sort used A Quicksort function which returns 1,0 or -1 depending on the result of the comparison as greater than, equal or less than respectively. I had assumed (incorrectly) that this applied to the Embarcadero C++ compilers especially as the compiler didn't issue any warnings or errors.