Solved

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

Posted on 2010-08-25
7
1,030 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

706 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now