• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Sort question

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
alexiat
Asked:
alexiat
1 Solution
 
monirCommented:
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
 
alexiatAuthor Commented:
Thanks.  Just wasn't sure which sort and data structure would be the most efficient.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now