Solved

Sort question

Posted on 2003-11-07
2
200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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

12 Experts available now in Live!

Get 1:1 Help Now