Recent

Author Topic: Sorting a listview  (Read 10419 times)

JD

  • Hero Member
  • *****
  • Posts: 1854
Sorting a listview
« on: November 17, 2011, 12:53:12 pm »
Hi there everyone,

I have some Delphi code for sorting a Listview

Code: [Select]
type
  TCustomSortStyle = (cssAlphaNum, cssNumeric, cssDateTime);

...........

case LvSortStyle of
    cssAlphaNum : Result := lstrcmp(PChar(s1), PChar(s2));
    cssNumeric  : begin
                    r1 := IsValidNumber(s1, i1);
                    r2 := IsValidNumber(s2, i2);
                    Result := ord(r1 or r2);
                    if Result <> 0 then
                      Result := CompareNumeric(i2, i1);
                  end;
    cssDateTime : begin
                    r1 := IsValidDate(s1, d1);
                    r2 := IsValidDate(s2, d2);
                    Result := ord(r1 or r2);
                    if Result <> 0 then
                      Result := CompareDates(d1, d2);
                  end;
  end;

The code fails to compile because of the line
Code: [Select]
Result := lstrcmp(PChar(s1), PChar(s2));

The problem is the 'lstrcmp' command. What is its equivalent in Lazarus/FreePascal?

Thank you for your kind assistance.

JD
Linux Mint - Lazarus 4RC3/FPC 3.2.2,
Windows - Lazarus 4RC3/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: Sorting a listview
« Reply #1 on: November 17, 2011, 01:06:21 pm »
strcomp, I think

JD

  • Hero Member
  • *****
  • Posts: 1854
Re: Sorting a listview
« Reply #2 on: November 17, 2011, 02:31:22 pm »
strcomp, I think

Thanks. It worked. But I'm wondering if using AnsiCompareText might not be a better choice so that I can properly sort UTF8 strings. What do you think?
Linux Mint - Lazarus 4RC3/FPC 3.2.2,
Windows - Lazarus 4RC3/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
Re: Sorting a listview
« Reply #3 on: November 17, 2011, 03:19:44 pm »
strcomp, I think

Thanks. It worked. But I'm wondering if using AnsiCompareText might not be a better choice so that I can properly sort UTF8 strings. What do you think?

ANSI and UTF8 are different things, AnsiCompareText will not work properly with UTF8 string.

JD

  • Hero Member
  • *****
  • Posts: 1854
Re: Sorting a listview
« Reply #4 on: November 17, 2011, 09:49:18 pm »
strcomp, I think

Thanks. It worked. But I'm wondering if using AnsiCompareText might not be a better choice so that I can properly sort UTF8 strings. What do you think?

ANSI and UTF8 are different things, AnsiCompareText will not work properly with UTF8 string.

WideCompareText should do the job, don't you think?
Linux Mint - Lazarus 4RC3/FPC 3.2.2,
Windows - Lazarus 4RC3/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

Shebuka

  • Sr. Member
  • ****
  • Posts: 429
Re: Sorting a listview
« Reply #5 on: November 18, 2011, 09:54:21 am »

JD

  • Hero Member
  • *****
  • Posts: 1854
Re: Sorting a listview
« Reply #6 on: November 18, 2011, 10:15:13 am »
Linux Mint - Lazarus 4RC3/FPC 3.2.2,
Windows - Lazarus 4RC3/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Sorting a listview
« Reply #7 on: November 18, 2011, 11:07:26 am »
We also have UTF8CompareText in the unit LazUTF8 (Lazarus 0.9.31+)

JD

  • Hero Member
  • *****
  • Posts: 1854
Re: Sorting a listview
« Reply #8 on: November 18, 2011, 01:34:20 pm »
We also have UTF8CompareText in the unit LazUTF8 (Lazarus 0.9.31+)

Think I'll prefer this just because of the way it is named. Thanks.
Linux Mint - Lazarus 4RC3/FPC 3.2.2,
Windows - Lazarus 4RC3/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: Sorting a listview
« Reply #9 on: November 18, 2011, 02:02:51 pm »
There are differences, WideCompareText uses the Unicode Manager to do the real work, which by default will call the Windows API in Windows and call the iconv library in UNIXes.

UTF8CompareText on the other hand uses UTF8LowerCase, which is a highly optimized routine written 100% in Pascal and which covers the *entire* Unicode specification. We made several rounds of optimization to tun it up, and I implemented support even for old greek and coptic =D Despite UTF8LowerCase being extremely fast (it is 2,5 times faster then iconv in my measures), the routine UTF8CompareText itself wasn't yet so much optimized, so it might be slower then iconv. But WideCompareStr works only in UTF-16 so you need to convert the string to use it, and because of that I think that UTF8CompareText in the end will have a similar performance to WideCompareText.

If you install the Pascal Unicode Manager (unit paswstring.pas in the package lazutils) then WideCompareStr will the routines written in Pascal to do the comparison, but it will be slower because you will get many conversions since WideCompareStr works in UTF-16.
« Last Edit: November 18, 2011, 02:04:33 pm by felipemdc »

JD

  • Hero Member
  • *****
  • Posts: 1854
Re: Sorting a listview
« Reply #10 on: November 19, 2011, 11:31:12 am »
@felipemdc
Thanks a lot for the detailed explanation.  I can see I have some catchup reading to do where Unicode strings are concerned.  :D
Linux Mint - Lazarus 4RC3/FPC 3.2.2,
Windows - Lazarus 4RC3/FPC 3.2.2

mORMot 2, PostgreSQL & MariaDB.

 

TinyPortal © 2005-2018