Solved

Delphi What is the best way to sort an Array of String

Posted on 2010-08-25
7
1,364 Views
Last Modified: 2013-11-23
What is the fastest sort to alphabetically sort an Array of String.  The arrays will generally be smalish, about 20 elements.  I am using Delphi 2010 and beleve there is a built in function to do this, if so what is the function and how is it used?
0
Comment
Question by:HenryM2
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 13

Expert Comment

by:aflarin
ID: 33519402
You're right. There is a built in function for array of string. But not for dynamic array. It is for TStrings class:

var
  StrArray: TStrings;
begin
  StrArray:= TStringList.Create;
  try
    StrArray.Add( 'Str 1' );
    StrArray.Add( 'Str 2' );
    StrArray.Add( 'Str 3' );
    StrArray.Sort; <------------------------
  finally
    StrArray.Free;
  end;


0
 
LVL 13

Expert Comment

by:aflarin
ID: 33519424
If you are using D2010, try something like this

TArray.Sort<string>(YourArray , TDelegatedComparer<string>.Construct(
  function(const Left, Right: string): Integer
  begin
    Result := TComparer<String>.Default.Compare(Left, Right);
  end));
0
 
LVL 13

Expert Comment

by:aflarin
ID: 33519501
Here is tested example:

uses
  Generics.Defaults,
  Generics.Collections;

procedure TForm1.Button1Click(Sender: TObject);
var
  StrArray: array of string;
begin
  SetLength( StrArray, 3);
  StrArray[0]:= 'Str 3';
  StrArray[1]:= 'Str 2';
  StrArray[2]:= 'Str 1';

  TArray.Sort<string>(StrArray , TDelegatedComparer<string>.Construct(
    function(const Left, Right: string): Integer
    begin
      Result := TComparer<String>.Default.Compare(Left, Right);
    end));
end;
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
aflarin earned 500 total points
ID: 33519536
Even shorter:

uses
  Generics.Defaults,
  Generics.Collections;

procedure TForm1.Button1Click(Sender: TObject);
var
  StrArray: array of string;
begin
  SetLength( StrArray, 3);
  StrArray[0]:= 'Str 3';
  StrArray[1]:= 'Str 2';
  StrArray[2]:= 'Str 1';

  TArray.Sort<string>(StrArray); // <----------------
end;
0
 

Author Comment

by:HenryM2
ID: 33519673
I am having difficulty to get it working.  I have not used TStrings before, is there a Unit to be listes in Uses for this. I added the Uses caluses that you used but get Incompatible type error in line "SetLength( StrArray, 3);"

I also get [DCC Error] SiteDesignerMainUnt.pas(9710): E2250 There is no overloaded version of 'TArray.Sort<System.string>' that can be called with these arguments for line " TArray.Sort<string>(StrArray);"
0
 

Author Comment

by:HenryM2
ID: 33519725
Sorry, my previous commnet = false alarm, now working.
0
 

Author Closing Comment

by:HenryM2
ID: 33519786
Thanks, works great.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question