Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sorting in Descending order:

690 views
Skip to first unread message

Finn Tolderlund

unread,
Jul 12, 2002, 4:27:52 PM7/12/02
to
Create your own sort routine.
Look in the help under TStringList.CustomSort.
--
Finn Tolderlund


"vm" <v...@gte.net> skrev i en meddelelse news:3d2f39cd_2@dnews...
> Sorting in Descending order:
> I have a text file that needs to be sorted in Descending order.
> But TstringList only sorts in Ascending order,

Erwien Saputra

unread,
Jul 12, 2002, 4:34:39 PM7/12/02
to
vm wrote:

> Sorting in Descending order:
>
> But i am sure there is an easier/more efficient way to do this.


Not necessarily easier but I think it is more efficient, TStringList has
CustomSort method.

Call this method, I believe that is a public method. This method expect
a parameter, pointer to function, TStringListSortCompare.

Write this function

function CompareDescending(List: TStringList; Index1, Index2: Integer):
Integer;
begin
Result := AnsiCompareText(List.Items[Index2], List.Items[Index1]);
end;

and call TStringList.CustomSort method. For more explanation, look at
Delphi help, TStringList.CustomSort.

Wien.

vm

unread,
Jul 12, 2002, 4:22:50 PM7/12/02
to
Sorting in Descending order:

Greetings;


I have a text file that needs to be sorted in Descending order.
But TstringList only sorts in Ascending order,

so what i have done is to:

1) Lines2Sort.LoadFromFile('UnSortedLinesIn.txt'); // Sorted = TRUE

2) Then Tack on a PREFIX starting with (Lines2Sort.Count - 1) in descending
order onto each Lines2Sort line. (TstringList, Lines2PREFIX, Sorted =
TRUE).

3) Then reLOAD Lines2Sort (Sorted = False)
minus the PREFIX, from Lines2PREFIX.

4) Lines2Sort.SaveToFile('DescSortedLinesIn.txt');

But i am sure there is an easier/more efficient way to do this.

Thanks in advance! ..Vern


--
ôżô
V e r n www.parentpresent.org


André Werlang

unread,
Jul 12, 2002, 4:52:31 PM7/12/02
to
Please, update the sort functions:

function StringListAnsiCompareDesc(List: TStringList; Index1, Index2:
Integer): Integer;
begin
Result := AnsiCompareText(List[Index2], List[Index1]);
end;

function StringListAnsiCompareAsc(List: TStringList; Index1, Index2:
Integer): Integer;
begin
Result := AnsiCompareText(List[Index1], List[Index2]);
end;


"André Werlang" <be...@terra.com.br> escreveu na mensagem
news:3d2f3eba_1@dnews...
> type
> TSortOrder = (soDescend, soAscend);
>
> TSortedStringList = class(TStringList)
> private
> FSortOrder: TSortOrder;
> public
> procedure Sort; override;
> property SortOrder: TSortOrder read FSortOrder write FSortOrder;
> end;
> ...
> function StringListAnsiCompareDesc(List: TStringList; Index1, Index2:
> Integer): Integer;
> begin
> Result := AnsiCompareText(List.FList^[Index2].FString,
> List.FList^[Index1].FString);
> end;
>
> function StringListAnsiCompareAsc(List: TStringList; Index1, Index2:
> Integer): Integer;
> begin
> Result := AnsiCompareText(List.FList^[Index1].FString,
> List.FList^[Index2].FString);
> end;
>
> procedure TSortedStringList.Sort;
> begin
> if SortOrder = soDescend then
> CustomSort(StringListAnsiCompareDesc)
> else
> CustomSort(StringListAnsiCompareAsc);
> end;
>
> Beppe.
>
>


André Werlang

unread,
Jul 12, 2002, 4:40:29 PM7/12/02
to
0 new messages