Board index » delphi » Question about sorting

Question about sorting

Use the following OnCompare handler:

procedure TForm1.ListView1Compare(Sender: TObject; Item1, Item2: TListItem;
  Data: Integer; var Compare: Integer);

  function GetMonthNumber(Month: String): Integer;
  begin
    for Result := Low(LongMonthNames) to High(LongMonthNames) do
      if Month = LongMonthNames[Result] then
        Exit;
    Result := High(LongMonthNames) + 1;
  end;

var
  i, j: Integer;
begin
  i := GetMonthNumber(Item1.Caption);
  j := GetMonthNumber(Item2.Caption);
  if i > j then
    Compare := 1
  else
  if i < j then
    Compare := -1
  else
    Compare := 0;
end;

"Dave McCormick" <no_m...@hotmail.NOSPAMPLEASE> schreef in bericht
news:9g0hlsk7nlvuhr5brvieba1ci3pkvmkv77@4ax.com...

Quote
> Hello,
> I'm stuck pretty bad here and I'm hoping someone can help me out. I've
> got a TreeView which is populated by the months of the year. (January,
> February, March etc.) I'm trying to get them to sort properly (i.e.
> January first then February then March etc.) but I have no clue how to
> go about doing it. Is there an API call that would simplify the whole
> thing?

> Thanks for reading my post!

 

Re:Question about sorting


In article <9g0hlsk7nlvuhr5brvieba1ci3pkvmk...@4ax.com>, Dave McCormick

Quote
<no_m...@hotmail.NOSPAMPLEASE> writes:
>I'm stuck pretty bad here and I'm hoping someone can help me out. I've
>got a TreeView which is populated by the months of the year. (January,
>February, March etc.) I'm trying to get them to sort properly (i.e.
>January first then February then March etc.) but I have no clue how to
>go about doing it. Is there an API call that would simplify the whole
>thing?

When you load the TTreeView use Items.????Object instead of Items.???? (ie
AddObject instead of Add, AddChildObject instead of AddChild) etc. Then add the
month number as the object (type-casting as a pointer) ie

Items.AddObject('April', pointer(4));

Typical code :-

Fill TreeView with 12 monthly nodes :-

var
  i : integer;
begin
  for i := Low(LongMonthNames) to High(LongMonthNames) do
    TreeView1.Items.AddObject(nil, LongMonthNames[i], pointer(i));

The Sort call :-

  TreeView1.CustomSort(TVSort, 0);

The compare procedure :-

function TVSort(lParam1, lParam2, lParamSort: Longint): integer stdcall;
begin
  Result := integer(TTreeNode(lParam1).Data) -
integer(TTreeNode(lParam2).Data);
end;

This shows just twelve nodes with each to a month. If you have many nodes (more
than 12) but you want to sort them by date you could use ...

pointer(trunc(MyDate))

... as the Data value.

The Data can be _any_ DWord/integer value - its up to you what you put in it
and how you compare it. Just cast it to a pointer going in, and to whatever
coming out, and write the appropriate comparison.

If you have any option in the matter, make it simple to compare, even if its
more complicated to create the node Data. Then the overall action is quicker,
because you create once, but sort many times.

Alan Lloyd
alangll...@aol.com

Other Threads