Solved

Sort question

Posted on 2003-11-07
2
199 Views
Last Modified: 2010-04-05
I have up to 4000 items to sort.  These items need to be sorted on 2 fields - first on a numeric field and then on a string field.  For example:

1,widget; 3,zebra; 1,apple should be sorted as 1,apple; 1,widget; 3,zebra.

I don't have a particular data structure in mind yet.  Could anyone tell me what will be the most efficient way and structure for this kind of sort.  There are a number of options available but I would like to minimize memory usage and maximize speed.  

Thanks for any guidance.
alexia
0
Comment
Question by:alexiat
2 Comments
 
LVL 2

Accepted Solution

by:
monir earned 500 total points
ID: 9703388
The fastest sort algorithm is Quick Sort, in Delphi we have TList, so declare a class for your Item,

TItem = clase

  Int: integer;
  str: string;

 constructor create( i: int; s: string);
end;

 then store the items in TList

MyList: TList;

for i:=1 to 4000
  MyList.Add ( TItem.Create (v1, V2));

finally use sort method inside TList.

MyList.Sort (@sortItem);

function sortItem  ( Item1,Item2:Pointer) :Integer;
begin
  result:=TItem (Item1).int - TItem (Item2).Int);
  if  result=0 then // Same Value
     result:=CompareText(TItem (Item1).Str,TItem (Item2).str) ;
end;


If you need more description I will send you a tiny sample of code.

Monir
0
 

Author Comment

by:alexiat
ID: 9703608
Thanks.  Just wasn't sure which sort and data structure would be the most efficient.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

757 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

22 Experts available now in Live!

Get 1:1 Help Now