[Lazarus] Lazarus support for Sorting is completely inadequate.

Benito van der Zander benito at benibela.de
Sat Dec 1 18:21:15 CET 2012


> and there are other available too.
Which and where?

In an old post, I wrote:

 > I recently grepped the fpc source to find a stable sort, and instead 
I only found QuickSort implementations.
 > Seven of them! In TFPList.Sort, TStringList.QuickSort, 
TFPSList.QuickSort, TFPBaseDitherer.QuickSort,  
TFPMedianCutQuantizer.QuickSort, TFPMedianCutQuantizer.QuickSortBoxes 
and paslzxcomp.qsort
 >Plus another one in the LCL in TCustomGrid.Sort.

I would also call that "completely inadequate", and in addition 
unnecessarily overcomplicated.

If all theses classes would use the same sorting function, you could 
just write a single comparison function,
for the languages you have...

Benito


On 12/01/2012 05:23 PM, Mattias Gaertner 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




More information about the Lazarus mailing list