Como ordenar um ListView
Caros amigos,
Tenho um ListView que recebe as informações dos arquivos de uma pasta. Ele lista: NOME_DO_ARQUIVO, TAMANHO e DATA_CRIAÇÃO. Preciso sempre ordenar estas informações pela data, em ordem descendente. Alguem poderia me ajudar na ordenação destes arquivos?
Desde já agradeço.
Tenho um ListView que recebe as informações dos arquivos de uma pasta. Ele lista: NOME_DO_ARQUIVO, TAMANHO e DATA_CRIAÇÃO. Preciso sempre ordenar estas informações pela data, em ordem descendente. Alguem poderia me ajudar na ordenação destes arquivos?
Desde já agradeço.
Alex Maia
Curtidas 0
Respostas
Alissonmelo
19/09/2005
Coloque esse método no onColumnClick do listView passando como parametreo ele mesmo e a coluna que foi clicada
procedure OrdenarColunaListView(ListView: TListView; Coluna: TListColumn);
const
ValideChars = [´0´..´9´];
// é um numero válido
function IsNumericValue(Value: String): Boolean;
const
ValideSignal = [´,´,´.´,´-´];
var
i: integer;
begin
Result := not (Value = EmptyStr);
if Result then
begin
for i := 1 to Length(Value) do
begin
Result := (Value[i] in (ValideChars + ValideSignal));
if not Result then Break;
end;
if Result then
Result := (Pos(´-´, Value) < 2);
end;
end;
// è uma data válida
function IsDateTimeValue(Value: String): Boolean;
const
ValideSignal = [´-´,´/´,´:´];
var
i: integer;
begin
Result := not (Value = EmptyStr);
if Result then
begin
for i := 1 to Length(Value) do
begin
Result := (Value[i] in (ValideChars + ValideSignal));
if not Result then Break;
end;
if Result then
Result := (Pos(#47, Value) = 3) and (Pos(47, Copy(Value, 4, 10)) = 3);
end;
end;
// Compara dusas strings formatadas em formato de numero
function CompareNumber(Value1, Value2: String): Integer;
var
v1, v2: Double;
begin
Result := 0;
try
v1 := StrToFloat(StringReplace(Value1, ´.´, EmptyStr, [rfReplaceAll]));
v2 := StrToFloat(StringReplace(Value2, ´.´, EmptyStr, [rfReplaceAll]));
if (v1 > v2) then
Result := 1
else if (v1 < v2) then Result := -1
except
Result := CompareText(Value1, Value2);
end;
end;
// Compara dusas strings formatadas em formato de data
function CompareDate(Value1, Value2: String): Integer;
var
v1, v2: Double;
begin
Result := 0;
try
v1 := StrToDateTime(Value1);
v2 := StrToDateTime(Value2);
if (v1 > v2) then
Result := 1
else if (v1 < v2) then Result := -1
except
Result := CompareText(Value1, Value2);
end;
end;
// Compara dois valores string
function CompareValues(Value1, Value2: String): Integer;
begin
if IsDateTimeValue(Value1) and IsDateTimeValue(Value2) then
Result := CompareDate(Value1, Value2)
else if IsNumericValue(Value1) and IsNumericValue(Value2) then
Result := CompareNumber(Value1, Value2)
else Result := CompareText(Value1, Value2);
end;
// Efetua a ordenação do ListView
function CustomSortListView(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var
Colunm: TListColumn;
begin
Colunm := TListColumn(ParamSort);
if Colunm.Index = 0 then
Result := CompareValues(Item1.Caption, Item2.Caption) * Colunm.Tag
else Result := CompareValues(Item1.SubItems[Colunm.Index - 1], Item2.SubItems[Colunm.Index - 1]) * Colunm.Tag;
end;
begin
if Coluna.Tag = 0 then
Coluna.Tag := 1;
ListView.CustomSort(@CustomSortListView, LongInt(Coluna));
Coluna.Tag := Coluna.Tag * -1;
end;
procedure OrdenarColunaListView(ListView: TListView; Coluna: TListColumn);
const
ValideChars = [´0´..´9´];
// é um numero válido
function IsNumericValue(Value: String): Boolean;
const
ValideSignal = [´,´,´.´,´-´];
var
i: integer;
begin
Result := not (Value = EmptyStr);
if Result then
begin
for i := 1 to Length(Value) do
begin
Result := (Value[i] in (ValideChars + ValideSignal));
if not Result then Break;
end;
if Result then
Result := (Pos(´-´, Value) < 2);
end;
end;
// è uma data válida
function IsDateTimeValue(Value: String): Boolean;
const
ValideSignal = [´-´,´/´,´:´];
var
i: integer;
begin
Result := not (Value = EmptyStr);
if Result then
begin
for i := 1 to Length(Value) do
begin
Result := (Value[i] in (ValideChars + ValideSignal));
if not Result then Break;
end;
if Result then
Result := (Pos(#47, Value) = 3) and (Pos(47, Copy(Value, 4, 10)) = 3);
end;
end;
// Compara dusas strings formatadas em formato de numero
function CompareNumber(Value1, Value2: String): Integer;
var
v1, v2: Double;
begin
Result := 0;
try
v1 := StrToFloat(StringReplace(Value1, ´.´, EmptyStr, [rfReplaceAll]));
v2 := StrToFloat(StringReplace(Value2, ´.´, EmptyStr, [rfReplaceAll]));
if (v1 > v2) then
Result := 1
else if (v1 < v2) then Result := -1
except
Result := CompareText(Value1, Value2);
end;
end;
// Compara dusas strings formatadas em formato de data
function CompareDate(Value1, Value2: String): Integer;
var
v1, v2: Double;
begin
Result := 0;
try
v1 := StrToDateTime(Value1);
v2 := StrToDateTime(Value2);
if (v1 > v2) then
Result := 1
else if (v1 < v2) then Result := -1
except
Result := CompareText(Value1, Value2);
end;
end;
// Compara dois valores string
function CompareValues(Value1, Value2: String): Integer;
begin
if IsDateTimeValue(Value1) and IsDateTimeValue(Value2) then
Result := CompareDate(Value1, Value2)
else if IsNumericValue(Value1) and IsNumericValue(Value2) then
Result := CompareNumber(Value1, Value2)
else Result := CompareText(Value1, Value2);
end;
// Efetua a ordenação do ListView
function CustomSortListView(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
var
Colunm: TListColumn;
begin
Colunm := TListColumn(ParamSort);
if Colunm.Index = 0 then
Result := CompareValues(Item1.Caption, Item2.Caption) * Colunm.Tag
else Result := CompareValues(Item1.SubItems[Colunm.Index - 1], Item2.SubItems[Colunm.Index - 1]) * Colunm.Tag;
end;
begin
if Coluna.Tag = 0 then
Coluna.Tag := 1;
ListView.CustomSort(@CustomSortListView, LongInt(Coluna));
Coluna.Tag := Coluna.Tag * -1;
end;
GOSTEI 0