Q&A

  • 질문 : Listbox 정렬.
listbox 에 데이타가 표시되는데 시간별로 정렬하고자 하는데 쉬운 것 같으면서 잘 안되네요.

예를 들어 리스트박스에



고객명, 고객번호, 구입물건, 구입시간, 으로 표시될 때 구입시간 순서대로 정렬하려면 어떻게 해야하는지 부탁드립니다.

2  COMMENTS
  • Profile
    최용일 2001.12.03 21:00
    안녕하세요. 최용일입니다.



    아래와 같이 해보세요...



    type

    TMySortCompare = function (Items: TStrings; Index1, Index2: Integer): Integer;



    type

    TForm1 = class(TForm)

    ListBox1: TListBox;

    Button1: TButton;

    procedure Button1Click(Sender: TObject);

    protected

    procedure Sort;

    procedure QuickSort(Items: TStrings; L, R: Integer; Compare: TMySortCompare);

    end;



    var

    Form1: TForm1;



    implementation



    {$R *.DFM}



    procedure TForm1.Button1Click(Sender: TObject);

    begin

    Sort;

    end;



    procedure TForm1.QuickSort(Items: TStrings; L, R: Integer; Compare: TMySortCompare);

    var

    I, J, P: Integer;

    begin

    repeat

    I := L;

    J := R;

    P := (L + R) shr 1;

    repeat

    while Compare(Items, I, P) < 0 do Inc(I);

    while Compare(Items, J, P) > 0 do Dec(J);

    if I <= J then

    begin

    Items.Exchange(I, J);

    if P = I then

    P := J

    else if P = J then

    P := I;

    Inc(I);

    Dec(J);

    end;

    until I > J;

    if L < J then QuickSort(Items, L, J, Compare);

    L := I;

    until I >= R;

    end;



    function CompareProc(Items: TStrings; Index1, Index2: Integer): Integer;

    begin

    // 여기에서 구입시간을 파싱해서 적절히 비교하시면됩니다...

    // Index1의 구입시간 > Index2의 구입시간 >>> Result = 1

    // Index1의 구입시간 = Index2의 구입시간 >>> Result = 0

    // Index1의 구입시간 < Index2의 구입시간 >>> Result = -1

    Result := CompareText(Items[Index1], Items[Index2]);

    end;



    procedure TForm1.Sort;

    begin

    if (ListBox1.Items.Count > 0) then

    QuickSort(ListBox1.Items, 0, ListBox1.Items.Count - 1, CompareProc);

    end;



    end.



    ^^ 항상 즐코하세요...



    prosit wrote:

    > listbox 에 데이타가 표시되는데 시간별로 정렬하고자 하는데 쉬운 것 같으면서 잘 안되네요.

    > 예를 들어 리스트박스에

    >

    > 고객명, 고객번호, 구입물건, 구입시간, 으로 표시될 때 구입시간 순서대로 정렬하려면 어떻게 해야하는지 부탁드립니다.

  • Profile
    prosit 2001.12.03 22:02
    전에도 몇 번 도움을 주시더니, 오늘도 도와주시는 군요.

    최 용일님께 도움을 받는 분들이 많은 것으로 알고 있는데....

    진심으로 감사드립니다.