[Lazarus] Lazarus support for Sorting is completely inadequate.

Avishai avishai.gore at gmail.com
Sat Dec 1 17:47:32 CET 2012


The problem is that there is no way to know which sort algorithm to
use for which language without a lot of experimenting.  It took quite
a bit of time for me to find one that worked for Hebrew.

But more than that,  for globalization/localization the Language may
not be known in advance.

An example:
Your app. is Globalized/Localized for String display Language.  But
you have a Sorted ListBox with Default Sort.  The user selects Russian
for his language.  The app. knows the language, but the  ListBox Sort
is wrong.

There should be a central Sort routine that can decide which Sort
method to use based on the Language.

I'm asking for help to achieve this.  I have no idea what sort methods
to use for non-Latin languages.  Russian, Croatian, Greek...  Once
completed and tested, it could be made a part of Lazarus, or at the
very least, some guide could be available to help others.

Apart from that, why have different sort methods for different
controls that give different results?  TListbox, TComboBox,
TStringList, TStringGrid all give different results.  And as far as I
know, there is no Numeric Sort.  Not hard to write, but why isn't it
there?

So I don't agree that Lazarus has good sort support.

On Sat, Dec 1, 2012 at 6:23 PM, Mattias Gaertner
<nc-gaertnma at netcologne.de> wrote:
> On Sat, 1 Dec 2012 16:43:34 +0200
> Avishai <avishai.gore at gmail.com> wrote:
>
>> 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.
>
> I'm not sure if I can follow you here. Maybe this helps:
>
> A string list can be sorted with a single line of code or you can
> write your own compare function. The default sort algorithm is
> quicksort and there are other available too.
> So support for "Sorting" is good.
>
> Maybe you are missing some "String compare functions"?
>
> There are many basic compare functions in the RTL, FCL and LazUtils.
> For instance CompareStr, CompareText, AnsiCompareText, UTF8CompareStr,
> UTF8CompareText.
> All these functions have their pros and cons.
>
> Keep in mind that some human languages have multiple official compare
> functions and it gets even more difficult when mixing languages
> (unicode).
>
>
>> 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;
>
> Maybe you are searching for a unit with lots of string compare
> functions?
>
> Mattias
>
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus



-- 
Shalom,
Avishai
avishai.gore at gmail.com
אבישי גוֹר




More information about the Lazarus mailing list