[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2010-08-25
7
Medium Priority
?
1,590 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
Industry Leaders: 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

649 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