1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93:
| unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type TForm1 = class(TForm) ListBox1: TListBox; Button1: TButton; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure FormCreate(Sender: TObject);
private public end;
var Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); begin Listbox1.Clear; Listbox1.Items.Add('8'); Listbox1.Items.Add('18'); Listbox1.Items.Add('15'); Listbox1.Items.Add('12'); Listbox1.Items.Add('6'); Listbox1.Items.Add('17'); end; function CompareInt(List:TStringlist; Index1, Index2:Integer):Integer; var d1, d2: Integer; r1, r2: Boolean; function IsInt(AString : string; var AInteger : Integer): Boolean; var Code: Integer; begin Val(AString, AInteger, Code); Result := (Code = 0); end; begin r1 := IsInt(List[Index1], d1); r2 := IsInt(List[Index2], d2); Result := ord(r1 or r2); if Result <> 0 then begin if d1 < d2 then Result := -1 else if d1 > d2 then Result := 1 else Result := 0; end else Result := lstrcmp(PChar(List[Index1]), PChar(List[Index2])); end;
procedure TForm1.Button1Click(Sender: TObject); var sl: TStringList; begin sl := TStringList.Create; try listbox1.Sorted := False; sl.Assign(listbox1.Items); sl.CustomSort(CompareInt); listbox1.Items.Assign(sl); finally sl.Free; end; end;
procedure TForm1.Button2Click(Sender: TObject); var n:String; m:Integer; begin n := Listbox1.Items.Strings[0]; m := StrToInt(n)-1; Listbox1.Items.Add(IntToStr(m)); Button1.Click; end; end. |