Board index » cppbuilder » How TList->Sort() function works ?
Damon Chandle
![]() CBuilder Developer |
Thu, 15 Aug 2002 03:00:00 GMT
How TList->Sort() function works ?
Hi Fabio,
Quote> If you know how to use le function Sort of TList class, write me. 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... // in source... int __fastcall NameSortProc(void* data1, void* data2); __fastcall TForm1::TForm1(TComponent* Owner) std::string* nameA = new std::string("Apple Jack"); m_FNameList->Add(nameA); Quote} { for (int index = 0; index < m_FNameList->Count; ++index) delete static_cast<std::string*>(m_FNameList->Items[index]); delete m_FNameList; Quote} { std::string* name1 = static_cast<std::string*>(data1); std::string* name2 = static_cast<std::string*>(data2); return -name1->compare(*name2); Quote} { 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} -- |