Board index » delphi » problem with sorted TStringList

problem with sorted TStringList

Hi

I have a sorted TStringList and am not getting the sorting I expected. What
am I doing wrong?

var
  s : TStringList;
begin
  s := TStringList.create;
  s.sorted := true;
  s.add('0');
  s.add('-');
// so far so good, because the list is sorted the '-' appears before the '0'
// which is what i expected since ord('-') < ord('0')

// now i add these 2 strings and i expected the same behaviour!
  s.add('01');
  s.add('-1');
  listbox1.Items.addstrings(s);
  s.free;

Thanks in advance
Trevor

 

Re:problem with sorted TStringList


Hi Trevor,

Quote
> I have a sorted TStringList and am not getting the sorting I expected.
> What am I doing wrong?

Sort uses AnsiCompareText to sort the list, which uses the CompareString
function of Windows:

"Typically, strings are compared using what is called a "word sort"
technique. In a word sort, all punctuation marks and other nonalphanumeric
characters, except for the hyphen and the apostrophe, come before any
alphanumeric character. The hyphen and the apostrophe are treated
differently than the other nonalphanumeric symbols, in order to ensure that
words such as "coop" and "co-op" stay together within a sorted list."

So basically, the hyphen is "ignored", explaning the "odd" result.

- daniel

Re:problem with sorted TStringList


Quote
"Daniel Rutten" <so...@no.email> wrote in message

news:1020377340.497729@cereal.attica.net.nz...

Quote
> Hi Trevor,

> > I have a sorted TStringList and am not getting the sorting I expected.
> > What am I doing wrong?

> Sort uses AnsiCompareText to sort the list, which uses the CompareString
> function of Windows:

> "Typically, strings are compared using what is called a "word sort"
> technique. In a word sort, all punctuation marks and other nonalphanumeric
> characters, except for the hyphen and the apostrophe, come before any
> alphanumeric character. The hyphen and the apostrophe are treated
> differently than the other nonalphanumeric symbols, in order to ensure
that
> words such as "coop" and "co-op" stay together within a sorted list."

> So basically, the hyphen is "ignored", explaning the "odd" result.

.. and everything I think I know goes smokin'! ;-)

Other Threads