"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,
> 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.
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
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.
>
>