Board index » cppbuilder » sorting StringList backwards...function fails..

sorting StringList backwards...function fails..


2004-02-26 07:49:27 AM
cppbuilder73
hi remy,
you posted me a snip to sort a StringList backwards...
the problem is the compiler drops an "Expression yntax Error"
for the following function:
int __fastcall ReverseOrder(TStringList *List, int Index1, Index2)
{
-return AnsiCompareStr(List->Strings[Index1],List->Strings[Index2]); //
HERE !
}
I didn't know that this "-return..." is possible...
Now I use the below code...but it only sorts "normal" NOT "backwards"..
what do I need to modify here....?
/************************************************************/
// CustomSort() is not supported under BCB3...thats why...
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);
}
int __fastcall ReverseOrder(TStringList *List, int Index1, int Index2)
{
return AnsiCompareStr(List->Strings[Index1],List->Strings[Index2]);
}
void __fastcall TfrmMainUnit::Button1Click(TObject *Sender)
{
TStringList *sList = new TStringList;
try
{
sList->AddStrings(Memo1->Lines);
StringListCustomSort(sList,ReverseOrder);
Memo1->Lines->Assign(sList);
}
__finally
{
delete sList;
}
 
 

Re:sorting StringList backwards...function fails..

"Oren Halvani" < XXXX@XXXXX.COM >wrote in message
Quote
int __fastcall ReverseOrder(TStringList *List, int Index1, Index2)
Index2 is missing a data type specifier - it needs an 'int':
int __fastcall ReverseOrder(TStringList *List, int Index1, int Index2)
Quote
I didn't know that this "-return..." is possible...
AnsiCompareStr() returns an int. I was merely returning the negative value
of it. The same effect as doing the following:
return (-1 * AnsiCompareStr(List->Strings[Index1],
List->Strings[Index2]));
When you multiply anything by -1, it simply changes the sign from
positive-to-negative or negative-to-positive. That is basic elementary
school math :-)
Quote
Now I use the below code...but it only sorts "normal" NOT "backwards"..
You did not return the opposite of the return value from AnsiCompareStr().
Gambit
 

Re:sorting StringList backwards...function fails..

Oren Halvani wrote:
Quote
you posted me a snip to sort a StringList backwards...

the problem is the compiler drops an "Expression yntax Error"
for the following function:

int __fastcall ReverseOrder(TStringList *List, int Index1, Index2)
{
-return AnsiCompareStr(List->Strings[Index1],List->Strings[Index2]); //
HERE !
}

I didn't know that this "-return..." is possible...
Well if the compiler complains, would it be possible ?
Ever heard of typo's ? There are at least two.
And the compiler indicates the exact line.
And do you remenber my post where I advised this:
"You can change ascending to decending by negating the
result of the compare function."
Hans.
 

{smallsort}

Re:sorting StringList backwards...function fails..

thank you very much Remy