1  a1
2  a11
3  a21
3  a21
2  a11
1  a2
需要排出以下顺序
1 a1
1 a2
2 a11
2 a12
3 a21
3 a22如果此数据在数据库还好办,但不是,是DELPHI里的一些数组内容.请问用什么方法排最快最好.
 
用LISTBOX或LISTVIEW可以吗?请问怎写?

解决方案 »

  1.   

    var
      FrmMain: TFrmMain;
      UpDown: array [0..3] of Boolean;procedure TFrmMain.ListView1ColumnClick(Sender: TObject;
      Column: TListColumn);
    var
      I: Integer;
    begin
      for I := 0 to listview1.Columns.Count - 1 do
        listview1.Columns[I].ImageIndex := -1;
      listview1.CustomSort(@CustomSortProc,Column.Index);
      //升序
     { if UpDown[Column.Index] = True then
        Column.ImageIndex := 0
      else
      //降序
        Column.ImageIndex := 1; }
      UpDown[Column.Index] := not UpDown[Column.Index];
    end;
    function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer):
      integer; stdcall;
    begin
      case ParamSort of
        0:
        begin
          if UpDown[0] = True then
            Result := CompareText(Item1.Caption,Item2.Caption)
          else
            Result := -CompareText(Item1.Caption,Item2.Caption);
        end;
        1:
        begin
          if UpDown[1] = True then
            Result := CompareText(Item1.SubItems[0],Item2.SubItems[0])
          else
            Result := -CompareText(Item1.SubItems[0],Item2.SubItems[0]);
        end;
        2:
        begin
          if UpDown[2] = True then
            Result := CompareText(Item1.SubItems[1],Item2.SubItems[1])
          else
            Result := -CompareText(Item1.SubItems[1],Item2.SubItems[1]);
        end;
        3:
        begin
          if UpDown[3] = True then
            Result := CompareText(Item1.SubItems[2],Item2.SubItems[2])
          else
            Result := -CompareText(Item1.SubItems[2],Item2.SubItems[2]);
        end;
      end;
    end;