How TList->Sort() function works ?

Hi Fabio,

Quote
> If you know how to use le function Sort of TList class, write me.
> I don't know how to write a custom Compare function and how to use it.

First thing to note is that the sort procedure cannot be a non-static class
member function.  The data that's passed to the sort procedure are the items in
your list.  Within this procedure, you need to cast the supplied pointers to the
correct type, then perform a comparison.  Here's a simple example for reversing
a list of strings...

// in header...
    TList* m_FNameList;

// in source...
#include <string>

int __fastcall NameSortProc(void* data1, void* data2);

__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    m_FNameList = new TList();

    std::string* nameA = new std::string("Apple Jack");
    std::string* nameB = new std::string("Boogie");
    std::string* nameC = new std::string("Cinema");
    std::string* nameD = new std::string("Disco");

    m_FNameList->Add(nameA);
    m_FNameList->Add(nameB);
    m_FNameList->Add(nameC);
    m_FNameList->Add(nameD);

Quote
}

__fastcall TForm1::~TForm1()
{
    for (int index = 0; index < m_FNameList->Count; ++index)
        delete static_cast<std::string*>(m_FNameList->Items[index]);

    delete m_FNameList;

Quote
}

int __fastcall NameSortProc(void* data1, void* data2)
{
    std::string* name1 = static_cast<std::string*>(data1);
    std::string* name2 = static_cast<std::string*>(data2);

    return -name1->compare(*name2);

Quote
}

void __fastcall TForm1::ShowNamesButtonClick(TObject *Sender)
{
    m_FNameList->Sort(NameSortProc);
    for (int index = 0; index < m_FNameList->Count; ++index)
    {
        std::string* name =
            static_cast<std::string*>(m_FNameList->Items[index]);
        ShowMessage(name->c_str());
    }

Quote
}

HTH.

--
Damon Chandler
http://bcbcaq.freeservers.com