简单排序(27分)

J

jack011

Unregistered / Unconfirmed
GUEST, unregistred user!
list列表数据是:
list:=TstringList.create;
list.add('[01:30][08:45]张老师')
list.add('[02:30][03:45]徐老师')
list.add('[04:45]李老师')
我想在memo中按时间照顺序输出:
[01:30]张老师
[02:30]徐老师
[03:45]徐老师
[04:45]李老师
[08:45]张老师
谢谢各位大峡
 
//排序方法
function MyListsort(IP1,IP2 :pointer):integer;
begin
if Pmylist(IP1)^.Field1 > Pmylist(IP2)^.Field1 then
result := 1
else
if Pmylist(IP1)^.Field1 = Pmylist(IP2)^.Field1 then
result := 0
else
result := -1 ;
end;
//建立排序列表
for i:=1 to 100000do
begin
New(Mylist);
Mylist.Field1 := random(100000);
Mylist.Field2 := IntToStr(i);
List.Add(Mylist);
MList.Add(IntToSTr(Mylist.Field1)+'_'+Mylist.Field2);
end;
//调用
list.Sort(MyListSort);
//用时 80 毫秒
 
一个TListBox控件,一个TMemo控件,TButton控件,代码如下:
type PListData = ^TListData;
TListData = record
Name:String;
StrTime:TStringList;
end;
private
{ Private declarations }
SL:TList;
procedure GetListValue();
procedure TForm1.GetListValue();
var i:Integer;
S,ST:String;
P:pListData;
begin
for i:=0 to ListBox1.Items.Count-1do
begin
S:=ListBox1.Items;
ST:='';
new(P);
P.StrTime:=TStringList.Create;
while S<>''do
begin
if (Pos('[',S)=0) and (Pos(']',S)=0) then
begin
P.Name:=S;Break;
end;
if S[1]='[' then
ST:='['
else
if S[1]=']' then
begin
if ST<>'' then
P.StrTime.Add(ST+']');
end else
ST:=ST+S[1];
S:=Copy(S,2,Length(S));
end;
SL.Add(P);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
SL:=TList.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var i,j:Integer;
P:pListData;
SLS:TStringList;
begin
GetListValue;
SLS:=TStringList.Create;
for i:=0 to SL.Count-1do
begin
P:=PListData(SL);
for j:=0 to P.StrTime.Count-1do
SLS.Add(P.StrTime[j]+P.Name);
end;
SLS.Sort;
for j:=0 to SLS.Count-1do
memo1.Lines.Add(SLS[j]);
end;
 
感谢两位兄台.问题解决了!
谢谢!
 

Similar threads

D
回复
0
查看
2K
DelphiTeacher的专栏
D
D
回复
0
查看
1K
DelphiTeacher的专栏
D
D
回复
0
查看
887
DelphiTeacher的专栏
D
D
回复
0
查看
2K
DelphiTeacher的专栏
D
顶部