?
Solved

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

Posted on 2010-08-25
7
Medium Priority
?
1,464 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Accepted Solution

by:
aflarin earned 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

752 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