既然你已經填充的TStringList
,我只想排序它使用其CustomSort()
方法,然後你可以通過它循環添加節點需要樹,如:
function SortFilesByMonthAndDay(List: TStringList; Index1, Index2: Integer): Integer;
var
Value1, Value2: Integer;
begin
Value1 := StrToInt(Copy(List[Index1], 6, 2));
Value2 := StrToInt(Copy(List[Index2], 6, 2));
if Value1 = Value2 then
begin
Value1 := StrToInt(Copy(List[Index1], 9, 2));
Value2 := StrToInt(Copy(List[Index2], 9, 2));
end;
Result := Value2 - Value1;
end;
var
I: Integer;
FileMonth, CurrentMonth: Integer;
CurrentMonthNode: TTreeNode;
begin
CurrentMonth := 0;
CurrentMonthNode := nil;
Files.CustomSort(@SortFilesByMonthAndDay);
for I := 0 to Files.Count-1 do
begin
FileMonth := StrToInt(Copy(Files[I], 6, 2));
if FileMonth <> CurrentMonth then
begin
CurrentMonth := FileMonth;
CurrentMonthNode := TreeView1.Items.Add(nil, SysUtils.LongMonthNames[CurrentMonth]);
end;
TreeView1.Items.AddChild(CurrentMonthNode, Files[I]);
end;
end;