Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I use TStringList->CustomSort in my program... 1

Status
Not open for further replies.

fire

Programmer
Jan 6, 2002
10
IR
How can I use TStringList->CustomSort in my program?
I found samples in Delphi, but was useless ..
Please give me a sample code in C++ Builder if you can.

Thanks.
 
Hello

Here is one,

<Cpp Unit>
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include &quot;cStringListCustomSort.h&quot;
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource &quot;*.dfm&quot;
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
int __fastcall TForm1::StringListSortCompare( TStringList* List, int Index1, int Index2 )
{
return List->Strings[Index2].AnsiCompare( List->Strings[Index1] );
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//descending sort
TStringList* SL = new TStringList;
SL->Assign( Memo1->Lines );
SL->CustomSort( StringListSortCompare );
Memo1->Lines->Assign( SL );
delete SL;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
//ascending sort
TStringList* SL = new TStringList;
SL->Assign( Memo1->Lines );
SL->Sorted=true;
Memo1->Lines->Assign( SL );
delete SL;
}
//---------------------------------------------------------------------------


<Units Header>
//---------------------------------------------------------------------------

#ifndef cStringListCustomSortH
#define cStringListCustomSortH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TMemo *Memo1;
TButton *Button1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
static int __fastcall StringListSortCompare( TStringList* List, int
Index1, int Index2 );
public: // User declarations

__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top