function MyCustomCompare(List: TStringList; Index1, Index2: Integer): Integer;
var
S1, S2: string;
N1, N2: Integer;
begin
S1 := List[index1];
S2 := List[index2];
Result := 0;
if S1 = S2 then
Exit;
N1 := Pos('-', S1); //---
if N1 = 0 then
begin
Result := -1;
Exit;
end;
N2 := Pos('-', S2); //---
if N2 = 0 then
begin
Result := 1;
Exit;
end;
N1 := StrToIntDef(Copy(S1, 1, N1 - 1), 0);
N2 := StrToIntDef(Copy(S2, 1, N2 - 1), 0);
if N1 = N2 then
Result := 0
else
if N1 > N2 then
Result := 1
else
if N1 < N2 then
Result := -1;
end;
procedure TForm1Button1Click(Sender: TObject);
var
sl: TStringList;
begin
sl := TStringListCreate;
try
slAssign(lst1Items);
slCustomSort(MyCustomCompare);
lst2Clear;
lst2ItemsAddStrings(sl);
finally
slFree;
end; // try
end;
来个效果图:
不用memo控件,采用edit控件程序:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R dfm}
procedure TForm1Button1Click(Sender: TObject);
var
st,s,ss:string;
a:array[1100] of integer;
i,j,k:integer;
p,q,t:integer;
begin
st:=edit1Text;
i:=0;
while pos(',',st)>0 do begin
s:=copy(st,1,pos(',',st)-1);
val(s,j,k);
inc(i);
a[i]:=j;
delete(st,1,pos(',',st));
end;
val(st,j,k);
inc(i);
a[i]:=j;
for p:=1 to i-1 do for q:=p+1 to i do
if a[p]>a[q] then begin t:=a[p]; a[p]:=a[q]; a[q]:=t; end;
str(a[1]:0,ss);
for p:=2 to i do begin str(a[p]:0,s); ss:=ss+','+s; end;
edit1Text:=ss;
end;
end
以上就是关于delphi listbox 排序全部的内容,包括:delphi listbox 排序、delphi对数字进行排序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)