BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sort rows of a StringGrid

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage)
View previous topic :: View next topic  
Author Message
kvgs
Guest





PostPosted: Thu Jan 29, 2004 12:51 pm    Post subject: Sort rows of a StringGrid Reply with quote




I am using a StringGrid which has 3 columns and about 50 rows.
The first 2 columns contain string values and the 3 column
contains a double value.I want to sort the rows of the
StringGrid according with the values at the 3 column. How can I
do this;
Back to top
Shon
Guest





PostPosted: Thu Jan 29, 2004 11:18 pm    Post subject: Re: Sort rows of a StringGrid Reply with quote



Make function that sorts rows (in any method, ie BbubbleSort or other)
by comparing desired values and exchanging the contents of the rows in
desired range, ie all rows.

If you need the simple sorting code, I will be happy to provide it to
You.

([-: May The Force Be With You! :-])
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Shon


"kvgs" <atsoumpalos (AT) yahoo (DOT) gr> wrote

Quote:

I am using a StringGrid which has 3 columns and about 50 rows.
The first 2 columns contain string values and the 3 column
contains a double value.I want to sort the rows of the
StringGrid according with the values at the 3 column. How can I
do this;


Back to top
JD
Guest





PostPosted: Fri Jan 30, 2004 1:17 am    Post subject: Re: Sort rows of a StringGrid Reply with quote




"kvgs" <atsoumpalos (AT) yahoo (DOT) gr> wrote:
Quote:
[...] I want to sort the rows of the StringGrid according
with the values at the 3 column.

The TStringList does not have a sort method so you have to
come up with your own sort. For a small number of rows, a
bubble sort will suffice but I would suggest to you that you
implement a quick sort that you can reuse in the future.

TStringList has a Sorted property that you can use to sort on
AnsiStrings in ascending order. If you need to sort in descending
order and you have BCB5 or BCB6, you can use the TStringList
CustomSort method. If you don't have ver5 or 6, you can implement
a quick sort that's included at the end of this post.

//-------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//-------------------------------------------------------------
// forward declare the sort function(s)
int __fastcall SortDescending( TStringList*, int, int );
//-------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//-------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject* Sender)
{
TStringGrid* pGrid = StringGrid1;
TStringList* pList = new TStringList;
AnsiString Separator = "@"; // you can add characters to make Seperator more unique

for( Row = pGrid->FixedRows; Row < pGrid->RowCount; ++Row )
{
// add the cell to sort first - followed by a seperator - followed by the entire row
pList->Add( pGrid->Cells[ ColumnToSort ][ Row ] + Seperator + pGrid->Rows[ Row ]->Text );
}

// sort the list
pList->CustomSort( SortDescending );

// set the grid
for( int x = 0; x < pList->Count; ++x )
{
pGrid->Rows[ x + pGrid->FixedRows ]->Text = pList->Strings[ x ].Delete( 1, AnsiPos( pList->Strings[ x ], Seperator ) );
}

delete pList;
}
//-------------------------------------------------------------
int __fastcall SortDescending( TStringList* pList, int x1, int x2 )
{
// look at othe AnsiCompare functions for case sensitivity and such
return AnsiStrComp( pList->Strings[ x2 ],c_str(), pList->Strings[ x1 ].c_str() );
// you could alsouse a different method to determine what to return
// such as converting the text to a float:
// return atof( pList->Strings[ x2 ].c_str() ) - atof( pList->Strings[ x1 ].c_str() );
}
//-------------------------------------------------------------

The following is the quick sort on a TStringList for those who
don't have BCB5 or 6 (from Gambit):

#if(__BORLANDC__ < 0x0550) // prior to BCB5

typedef int __ fastcall (*TStringListSortCompare)(TStringList *List, int Index1, int Index2);

void __fastcall StringListQuickSort(TStringList *List, int L, int R, TStringListSortCompare SCompare)
{
int I, J, P;

do
{
I = L;
J = R;
P = ((L + R) >> 1);
do
{
while( SCompare(List, I, P) < 0 ) ++I;
while( SCompare(List, J, P) > 0 ) --J;
if( I <= J )
{
List->Exchange(I, J);
if( P == I )
P = J;
else if( P == J )
P = I;
++I;
--J;
}
}
while( I <= J );
if( L < J ) StringListQuickSort(List, L, J, SCompare);
L = I;
}
while( I < R );
}


void __fastcall StringListCustomSort(TStringList *List, TStringListSortCompare SCompare)
{
StringListQuickSort(List, 0, List->Count-1, SCompare);
}


#endif

int __fastcall MySortCallback(TStringList *List, int Index1, int Index2)
{
return List->Strings[Index2].Length() - List->Strings[Index1].Length();
}

void __fastcall TFormular::Button1Click(TObject *Sender)
{
TStringList *sList = new TStringList;
try
{
sList->AddStrings(Memo1->Lines);
#if(__BORLANDC__ < 0x0550) // prior to BCB5
StringListCustomSort(sList, MySortCallback);
#else
sList->CustomSort(MySortCallback);
#endif
Memo1->Lines->Assign(sList);
}
__finally
{
delete sList;
}
}

~ JD


Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (VCL Components Usage) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.