需要对采集到的数据进行排序,数据样本如下
ID,Volume,Price
a1,143234,0.0142
a2,454821,0.0134
a3,456785,0.0222
b1,112100,0.0336
b2,123299,0.0110这样子的数据
我想按price进行排序,不知道要怎么弄。。麻烦帮我写出一段代码。。
如果可以直接用的,你可以要求我再加200元。
ID,Volume,Price
a1,143234,0.0142
a2,454821,0.0134
a3,456785,0.0222
b1,112100,0.0336
b2,123299,0.0110这样子的数据
我想按price进行排序,不知道要怎么弄。。麻烦帮我写出一段代码。。
如果可以直接用的,你可以要求我再加200元。
SysUtils,
Generics.Defaults,
Generics.Collections;type
TRecord = record
ID: string;
Volume: Integer;
Price: Double;
end; TRecordSet = array of TRecord;var
_RecordSet: TRecordSet;
_Record: TRecord;
begin
SetLength(_RecordSet, 5); _RecordSet[0].ID := 'a1';
_RecordSet[0].Volume := 143234;
_RecordSet[0].Price := 0.0142; _RecordSet[1].ID := 'a2';
_RecordSet[1].Volume := 454821;
_RecordSet[1].Price := 0.0134; _RecordSet[2].ID := 'a3';
_RecordSet[2].Volume := 456785;
_RecordSet[2].Price := 0.0222; _RecordSet[3].ID := 'b1';
_RecordSet[3].Volume := 112100;
_RecordSet[3].Price := 0.0336; _RecordSet[4].ID := 'b2';
_RecordSet[4].Volume := 123299;
_RecordSet[4].Price := 0.0110; WriteLn('排序前');
for _Record in _RecordSet do
WriteLn(Format('%s %6d %.4f', [_Record.ID, _Record.Volume, _Record.Price])); TArray.Sort<TRecord>(_RecordSet, TDelegatedComparer<TRecord>.Create(
function(const Left, Right: TRecord): Integer
begin
Result := TComparer<Double>.Default.Compare(Left.Price, Right.Price);
end)); WriteLn('排序后');
for _Record in _RecordSet do
WriteLn(Format('%s %6d %.4f', [_Record.ID, _Record.Volume, _Record.Price])); ReadLn;end.
SQL:='select * from 表名 Order by ''price''';
type
PRData = ^RData;
RData = packed record
ID:string;
Volume:string;
Price:Double;
end;
function ReListSort(Apt1, Apt2: Pointer): Integer;
begin
if PRData(APt1)^.Price > PRData(APt2)^.Price then
Result := 1
else
if PRData(APt1)^.Price = PRData(APt2)^.Price then
Result := 0
else
Result := -1
end;
//调用
var
ItemList:TList;
ItemList.Sort(ReListSort);原来用过的,没有试过数量很大的。(Delphi 7)
把你这段数据最后一列提出来放到一个TStringList里面,然后调用Sort方法。简单好用。
/////////////////////////////////////////////////////////////////////
interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
PSomeRec = ^TSomeRec;
TSomeRec = packed record
FID: string;
FVol: Integer;
FPrice: Double;
end; TForm1 = class(TForm)
btn1: TButton;
btn2: TButton;
mmo1: TMemo;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function CompreRule(p1, p2: Pointer): Integer;
begin
if PSomeRec(p1).FPrice < PSomeRec(p2).FPrice then
Result := 1
else
Result := -1;
end;
{
a1,143234,0.0142
a2,454821,0.0134
a3,456785,0.0222
b1,112100,0.0336
b2,123299,0.0110}
procedure TForm1.btn2Click(Sender: TObject);
var
recLst: TList;
p: PSomeRec;
I: Integer;
s: string;
begin
recLst := TList.Create;
New(p);
P.FID := 'a1';
P.FVol := 143234;
P.FPrice := 0.0142;
recLst.Add(P); p := nil;
New(p);
p.FID := 'a2';
p.FVol := 454821;
p.FPrice := 0.0134;
recLst.Add(p); p := nil;
New(p);
p.FID := 'a3';
p.FVol := 456785;
p.FPrice := 0.0222;
recLst.Add(p); p := nil;
New(p);
p.FID := 'b1';
p.FVol := 112100;
p.FPrice := 0.0336;
recLst.Add(P); p := nil;
New(p);
p.FID := 'b2';
p.FVol := 123299;
p.FPrice := 0.0110;
recLst.Add(p); recLst.Sort(CompreRule); for I := 0 to recLst.Count - 1 do
begin
s := Format('%s%s%d%s%0.4f',
[PSomeRec(recLst[i]).FID,
#9,
PSomeRec(recLst[i]).FVol,
#9,
PSomeRec(recLst[i]).FPrice
]);
mmo1.Lines.Add(s);
Dispose(PSomeRec(recLst[i]));
end;
{输出结果如下:
b1 112100 0.0336
a3 456785 0.0222
a1 143234 0.0142
a2 454821 0.0134
b2 123299 0.0110
}
recLst.Clear;
FreeAndNil(recLst);
end;
StringList的数据是字符串,那是根据什么排序啊?TList行,貌似是快速排序