Board index » delphi » Howto sort '+', '-', '*', '/',... with Delphi3

Howto sort '+', '-', '*', '/',... with Delphi3

Hi'

        I want to sort the following string '+', '-', '*', '/', '->', '|' ,
'<<' and '='. So I've imagined using TstringList :

begin
WL := TStringList.Create;
WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
MotsClesExistants.sorted := True;
end;

But the result is :
'-', '*', '/', '|', '+', '<<', '=', '->'
here are ascii-code for the first letter :
2d   2a   2f  7c   2b    3c    3d   2d

Do you understand the order ? How to obtain ascii-order ?

HIL !
--
***Nouvelle Adresse********** Denis.Bouhin...@irin.univ-nantes.fr
IRIN- 2 rue de la houssiniere  Perso- Denis B. & Evelyne Chevigny
      BP 92 208                     5 place de la vieille Horloge
      44 322 Nantes Cedex 3   -France-    85 000 La Roche sur Yon
      (332/02) 40 37 49 71                   (332/02) 51 08 94 91

 

Re:Howto sort '+', '-', '*', '/',... with Delphi3


Quote
Denis Bouhineau wrote:

> begin
> WL := TStringList.Create;
> WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
> WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
> MotsClesExistants.sorted := True;
> end;

Instead of sorting MotsClesExistants, I think it would be better to
write:

begin
WL := TStringList.Create;
WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
WL.sorted := True;
end;

Marc

Re:Howto sort '+', '-', '*', '/',... with Delphi3


Quote
Marc Billiet wrote:

ok there was a small error in the original post, but the error was not
in the program ...
(the problem is still open)

it was about :

Quote

> Denis Bouhineau wrote:

> > begin
> > WL := TStringList.Create;
> > WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
> > WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
> > MotsClesExistants.sorted := True;
> > end;

> Instead of sorting MotsClesExistants, I think it would be better to
> write:

> begin
> WL := TStringList.Create;
> WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
> WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
> WL.sorted := True;
> end;

> Marc

--
***Nouvelle Adresse********** Denis.Bouhin...@irin.univ-nantes.fr
IRIN- 2 rue de la houssiniere  Perso- Denis B. & Evelyne Chevigny
      BP 92 208                     5 place de la vieille Horloge
      44 322 Nantes Cedex 3   -France-    85 000 La Roche sur Yon
      (332/02) 40 37 49 71                   (332/02) 51 08 94 91

Re:Howto sort '+', '-', '*', '/',... with Delphi3


Hi'

        I want to sort the following string '+', '-', '*', '/', '->',
'|' ,
'<<' and '='. So I've imagined using TstringList :

begin
WL := TStringList.Create;
WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
WL.sorted := True;
end;

But the result is :
'-', '*', '/', '|', '+', '<<', '=', '->'
here are ascii-code for the first letter :
2d   2a   2f  7c   2b    3c    3d   2d

Do you understand the order ? How to obtain ascii-order ?

HIL !

Re:Howto sort '+', '-', '*', '/',... with Delphi3


To sort the stringlist, you need to use its 'sort' *method* or set the
stringlist to sorted before adding the strings..

Changing the value of the 'sorted' property doesn't force immediate
sorting of the stringlist, it only means that strings added *after*
setting the property to true will be added in the correct order.

So :

1. to sort a stringlist at any time, use WL.sort;

2. To do what you probably want to do, use :
 begin
   WL := TStringList.Create;
   wl.sorted := true;
   wl.duplicates := dupIgnore; // if you want it to not raise an error
                                        //  if a string added twice.
   WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
   WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
 end;

Hope this helps

Steve

Quote
Denis Bouhineau <Denis.Bouhin...@irin.univ-nantes.fr> wrote:
>Marc Billiet wrote:

>> begin
>> WL := TStringList.Create;
>> WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
>> WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
>> WL.sorted := True;
>> end;

>> Marc

Re:Howto sort '+', '-', '*', '/',... with Delphi3


this solution (below) does'nt work !

Quote
Steve Gouldstone wrote:

> 2. To do what you probably want to do, use :
>  begin
>    WL := TStringList.Create;
>    wl.sorted := true;
>    wl.duplicates := dupIgnore; // if you want it to not raise an error
>                                         //  if a string added twice.
>    WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
>    WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
>  end;

> Hope this helps

> Steve

--
***Nouvelle Adresse********** Denis.Bouhin...@irin.univ-nantes.fr
IRIN- 2 rue de la houssiniere  Perso- Denis B. & Evelyne Chevigny
      BP 92 208                     5 place de la vieille Horloge
      44 322 Nantes Cedex 3   -France-    85 000 La Roche sur Yon
      (332/02) 40 37 49 71                   (332/02) 51 08 94 91

Re:Howto sort '+', '-', '*', '/',... with Delphi3


Quote
Denis Bouhineau wrote...
>Hi'

> I want to sort the following string '+', '-', '*', '/', '->', '|' ,
>'<<' and '='. So I've imagined using TstringList :

>begin
>WL := TStringList.Create;
>WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
>WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
>MotsClesExistants.sorted := True;
>end;

>But the result is :
>'-', '*', '/', '|', '+', '<<', '=', '->'
>here are ascii-code for the first letter :
>2d   2a   2f  7c   2b    3c    3d   2d

>Do you understand the order ? How to obtain ascii-order ?

I get exactly the same sorting order as you do. I'm using the english
version of D2 on a german Windows 95(A). I suppose you have a
different configuration. I expected it to be a code page problem
first, but then we would have had slightly different results. The
problem also appears in a list box. The same order again.

So I looked at the sources a bit. The TStringList and other lists seem
to use a quick sort routine to sort everything out. The quick sort
routine uses AnsiCompareText to compare the strings. I suppose the
problem lies in AnsiCompareText or perhaps in TStringList.QuickSort.

Delphi help says:

|Declaration
|
|function AnsiCompareText(const S1, S2: string): Integer;
|
|Description
|
|AnsiCompareText compares S1 to S2, without case sensitivity. The
compare operation is |controlled by the currently installed language
driver. The return value is the same as for |CompareStr.
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The sources show that AnsiCompareText uses:
  Result := CompareString(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
    PChar(S1), Length(S1), PChar(S2), Length(S2)) - 2;

So the problem is really the installed language driver

So blame M$.

Rudy Velthuis

Re:Howto sort '+', '-', '*', '/',... with Delphi3


Wow. I must admit I didn't try it, I just wrote what I though *should*
work. Silly me. Does it work in delphi 1? Haven't tried it.

It seems that the stringlists are case insensitive (ie they sort
alphabetically rather than ASCII-ey) when it comes to sorting the
added strings. For example it puts added strings in the order :-

aa
AB
xx

when we expect it to be :-

AB
aa
xx

Presumably it gets confused with the punctuation marks because it
ignores bit 6 of each character (to ignore case).

I would call this a bug. At the very least the documentation is being
extremely coy.

I'm glad you've spotted this, now I probably have to do some re-work
on my applications!

Steve

Quote
Denis Bouhineau <Denis.Bouhin...@irin.univ-nantes.fr> wrote:
>this solution (below) does'nt work !

>Steve Gouldstone wrote:

>> 2. To do what you probably want to do, use :
>>  begin
>>    WL := TStringList.Create;
>>    wl.sorted := true;
>>    wl.duplicates := dupIgnore; // if you want it to not raise an error
>>                                         //  if a string added twice.
>>    WL.Add('+');WL.Add('-');WL.Add('*');WL.Add('/');
>>    WL.Add('=');WL.Add('->');WL.Add('|');WL.Add('<<');
>>  end;

Re:Howto sort '+', '-', '*', '/',... with Delphi3


gou...@globalnet.co.uk wrote on 19.12.97 in article <349f4baa.124865...@read.news.global.net.uk>:

Quote
> [...]
> Changing the value of the 'sorted' property doesn't force immediate
> sorting of the stringlist, it only means that strings added *after*
> setting the property to true will be added in the correct order.

 That's not true! Setting "Sorted" to "True" forces an immediate
 "Sort()" of the existing stringlist. Just have a look at the
 source-code of "Classes.Pas" ...

--
        Matthias

Other Threads