WitkorDelphi - pamiętasz słowa Linusa Torwaldsa? Jeżeli potrzebujesz więcej niż trzy wcięcia w kodzie - spieprzyłeś go tak czy inaczej...
Opi, sprawdź jeszcze ten kod. Pominąłem w nim upierdliwe sprawdzanie poprawności danych. Bardziej chodzi mi o samą koncepcję programu. Traktuj jak szablon. Opakuj w klasy itp. Pisałem na szybko.
Tak wyglądają itemy:
34,21 kB
527,20 kB
12,01 MB
0 b
6,42 MB
267 B
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TCompareProc = function(Sender: TListBox; row1, row2: Integer): Integer;
TSwapProc = procedure(Sender: TListBox; row1, row2: Integer);
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function StrToSize(const s: string): Integer;
var
DelimiterPos :Integer;
Value, Coeff :Single;
CoeffPart :string;
begin
DelimiterPos := Pos(' ', s);
Value := StrToFloatDef(Copy(s, 1, DelimiterPos-1), -1);
CoeffPart := AnsiUpperCase(Copy(s, DelimiterPos+1, Length(s)));
if CoeffPart = 'B' then Coeff := 1
else
if CoeffPart = 'KB' then Coeff := 1024
else
if CoeffPart = 'MB' then Coeff := 1024*1024
else
if CoeffPart = 'GB' then Coeff := 1024*1024*1024
else
Coeff := -1;
if (Value = -1) or (Coeff = -1) then
Result := -1 else Result := Round(Value * Coeff);
end;
/////////////////////////////////////////////////////////////////////////////
function CompareInt(v1, v2 :Integer) :Integer;
begin
if v1 > v2 then Result := 1;
if v1 = v2 then Result := 0;
if v1 < v2 then Result := -1;
end;
/////////////////////////////////////////////////////////////////////////////
function NumCompareProc(Sender: TListBox; row1, row2: Integer): Integer;
begin
with Sender do
begin
Result := CompareInt(StrToSize(Items[row1]), StrToSize(Items[row2]));
if Result <> 0 then
begin
if (Items[row1] = '') then Result := 1
else
if (Items[row2] = '') then Result := -1
end
else
Result := row1 - row2;
end;
end;
/////////////////////////////////////////////////////////////////////////////
procedure SwapProc(Sender: TListBox; row1, row2: Integer);
var
s: string;
begin
with Sender do begin
s := Items[row1];
Items[row1] := Items[row2];
Items[row2] := s;
end;
end;
/////////////////////////////////////////////////////////////////////////////
procedure QuickSort(var ListBox :TListBox; bottom, top: Integer; Compare: TCompareProc; Swap: TSwapProc);
var
up, down, pivot: integer;
begin
down := top;
up := bottom;
pivot := (top + bottom) div 2;
repeat
while compare(ListBox, up, pivot) < 0 do Inc(up);
while compare(ListBox, down, pivot) > 0 do Dec(down);
if up <= down then
begin
swap(ListBox, up, down);
if pivot = up then pivot := down
else
if pivot = down then pivot := up;
Inc(up);
Dec(down);
end;
until up > down;
if bottom < down then
quickSort(ListBox, bottom, down, compare, swap);
if up < top then
quickSort(ListBox, up, top, compare, swap);
end;
/////////////////////////////////////////////////////////////////////////////
procedure Sort(var ListBox :TListBox; Compare: TCompareProc; Swap: TSwapProc);
begin
if not Assigned(Compare) then Compare := NumCompareProc;
if not Assigned(Swap) then Swap := SwapProc;
QuickSort(ListBox, 0, ListBox.Items.Count-1, Compare, Swap);
end;
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
var
C :TCompareProc;
S :TSwapProc;
begin
C := NumCompareProc;
S := SwapProc;
Sort(ListBox1, C, S);
end;
end.