[Lazarus] Lazarus support for Sorting is completely inadequate.
Avishai
avishai.gore at gmail.com
Sat Dec 1 15:43:34 CET 2012
Lazarus support for Sorting is completely inadequate. You can not
sort a list of strings without knowing the language of the list. This
should be a simple process. I would like to try to improve this, but I
will need some help from the Lazarus community.
It appears that each Control that has a List has its own separate Sort
routine and each is different. I would think that there would be a
single Sort routine and could be called by each control.
This could be a starting point that could be expanded to include other
Languages and Sort Methods, but it is only a suggestion.
{===== Sorting =====}
const
SortAscending: Boolean = True;
function SortHebrew(List: TStringList; Index1, Index2: Integer):
Integer; {Hebrew}
begin
Result:= UTF8CompareStr(List[Index1],List[Index2]);
if not SortAscending then
Result:= -Result;
end;
function SortPortuguese(List: TStringList; Index1, Index2: Integer):
Integer; {Portuguese}
begin
Result:= WideCompareText(UTF8Decode(List[Index1]),UTF8Decode(List[Index2]));
if not SortAscending then
Result:= -Result;
end;
{===== Numeric Sort =====}
function DoNumSort(List: TStringList; Index1, Index2: Integer): Integer;
var
N1, N2: Double;
begin
N1:= StrToFloatDef(List[Index1],0);
N2:= StrToFloatDef(List[Index2],0);
if N1 < N2 then
result := -1
else if N1 > N2 then
result := 1
else
result := 0;
if not SortAscending then
Result:= -Result;
end;
procedure SortNumeric(Items: TStrings);
var
Alist: TStringList;
begin
Alist:= TStringList.Create;
with Items do begin
Alist.CommaText:= CommaText;
Alist.CustomSort(@DoNumSort);
CommaText:= Alist.commaText;
end;
FreeAndNil(Alist);
end;
{ This is the User Sort main routine }
{ Example Call: SortStringList(Memo1.Lines, HebrewSort); }
{ Example Call: SortStringList(ListBox1.Items, HebrewSort); }
{ Example Call: SortStringList(ComboBox1.Items, HebrewSort); }
function SortStringList(AStringList: TStrings; SortStyle: Integer): Integer;
var
SortList: TStringList;
begin
SortList:= TStringList.Create;
SortList.CaseSensitive:= True;
SortList.Assign(AStringList);
{ Sort according to Language }
case SortStyle of
EnglishSort: SortList.Sort;
HebrewSort: SortList.CustomSort(@SortHebrew);
PortugueseSort: SortList.CustomSort(@SortPortuguese);
NumericSort: SortList.CustomSort(@DoNumSort);
{ need input from Lazarus community for other Sort Methods }
end;
AStringList.Assign(SortList);
FreeAndNil(SortList);
end;
--
Shalom,
Avishai
avishai.gore at gmail.com
אבישי גוֹר
More information about the Lazarus
mailing list