Esopo
asked on
Sorting record structure
Hi,
I am using a simple record structure:
type
KeyAnalyze=Record
Key:string;
Num:integer;
end;
After populating the records, How can I sort in descending order by the "Num" value?
I am using a simple record structure:
type
KeyAnalyze=Record
Key:string;
Num:integer;
end;
After populating the records, How can I sort in descending order by the "Num" value?
where do you populate the records?
ASKER
var
KeyArray: array of KeyAnalyze;
...
then, within a loop:
SetLength(KeyArray, length(KeyArray) +1);
KeyArray[length(KeyArray) -1].Key:= WhateverString;
KeyArray[length(KeyArray) -1].Num:= WhateverNumber;
*********
Now I need to sort the array of records by the Num value so that I can return the records organized in descending order to the user.
Makes sense?
KeyArray: array of KeyAnalyze;
...
then, within a loop:
SetLength(KeyArray, length(KeyArray) +1);
KeyArray[length(KeyArray) -1].Key:= WhateverString;
KeyArray[length(KeyArray) -1].Num:= WhateverNumber;
*********
Now I need to sort the array of records by the Num value so that I can return the records organized in descending order to the user.
Makes sense?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you!
I was hoping for a SortStructure() kind of function from Delphi, but your is just as good.
I was hoping for a SortStructure() kind of function from Delphi, but your is just as good.
well if you have the enterprise version you might be able to use TBaseArray and override the Sort() method,
otherwise you could use a TObjectList and a class to do the same thing (see below)
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
contnrs; // needed for TObjectList which automatically frees list items
type
TKeyAnalyze = class
public
Key:string;
Num:integer;
Constructor Create(Key_: string; Num_: integer);
end;
type
TForm2 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyList: TObjectList;
end;
var
Form2: TForm2;
function SortByNum(Item1, Item2: Pointer): Integer;
function SortByNumDescending(Item1, Item2: Pointer): Integer;
IMPLEMENTATION
{$R *.DFM}
{ sort routines }
function SortByNum(Item1, Item2: Pointer): Integer;
begin
if TKeyAnalyze(Item1).Num < TKeyAnalyze(Item2).Num then
result := -1
else
if TKeyAnalyze(Item1).Num > TKeyAnalyze(Item2).Num then
result := 1
else
result := 0;
end;
function SortByNumDescending(Item1, Item2: Pointer): Integer;
begin
if TKeyAnalyze(Item1).Num > TKeyAnalyze(Item2).Num then
result := -1
else
if TKeyAnalyze(Item1).Num < TKeyAnalyze(Item2).Num then
result := 1
else
result := 0;
end;
{ KeyAnalyze }
constructor TKeyAnalyze.Create(Key_: string; Num_: integer);
begin
inherited Create;
Key := Key_;
Num := Num_;
end;
{ Form }
procedure TForm2.FormCreate(Sender: TObject);
var
i: integer;
begin
MyList := TObjectList.Create;
MyList.add( TKeyAnalyze.Create('A', 4) );
MyList.add( TKeyAnalyze.Create('B', 2) );
MyList.add( TKeyAnalyze.Create('C', 1) );
MyList.add( TKeyAnalyze.Create('D', 3) );
MyList.Sort(SortByNumDesce nding);
for i := 0 to MyList.Count - 1 do
memo1.lines.add( TKeyAnalyze(MyList[i]).Key + ' ' + IntToStr( TKeyAnalyze(MyList[i]).Num ) );
end;
procedure TForm2.FormDestroy(Sender: TObject);
begin
MyList.Free;
end;
end.
otherwise you could use a TObjectList and a class to do the same thing (see below)
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls,
contnrs; // needed for TObjectList which automatically frees list items
type
TKeyAnalyze = class
public
Key:string;
Num:integer;
Constructor Create(Key_: string; Num_: integer);
end;
type
TForm2 = class(TForm)
Memo1: TMemo;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
MyList: TObjectList;
end;
var
Form2: TForm2;
function SortByNum(Item1, Item2: Pointer): Integer;
function SortByNumDescending(Item1,
IMPLEMENTATION
{$R *.DFM}
{ sort routines }
function SortByNum(Item1, Item2: Pointer): Integer;
begin
if TKeyAnalyze(Item1).Num < TKeyAnalyze(Item2).Num then
result := -1
else
if TKeyAnalyze(Item1).Num > TKeyAnalyze(Item2).Num then
result := 1
else
result := 0;
end;
function SortByNumDescending(Item1,
begin
if TKeyAnalyze(Item1).Num > TKeyAnalyze(Item2).Num then
result := -1
else
if TKeyAnalyze(Item1).Num < TKeyAnalyze(Item2).Num then
result := 1
else
result := 0;
end;
{ KeyAnalyze }
constructor TKeyAnalyze.Create(Key_: string; Num_: integer);
begin
inherited Create;
Key := Key_;
Num := Num_;
end;
{ Form }
procedure TForm2.FormCreate(Sender: TObject);
var
i: integer;
begin
MyList := TObjectList.Create;
MyList.add( TKeyAnalyze.Create('A', 4) );
MyList.add( TKeyAnalyze.Create('B', 2) );
MyList.add( TKeyAnalyze.Create('C', 1) );
MyList.add( TKeyAnalyze.Create('D', 3) );
MyList.Sort(SortByNumDesce
for i := 0 to MyList.Count - 1 do
memo1.lines.add( TKeyAnalyze(MyList[i]).Key
end;
procedure TForm2.FormDestroy(Sender:
begin
MyList.Free;
end;
end.
ASKER
Very interesting. Is this kind of power that makes me proud of using Delphi :D