Q&A

  • TList클래스에 Sort방법?
안녕하세요.

TList클래스를 이용해 파일리스트를 만들어

화면에 출력할려고 하는데..

이걸.. 이름순(FileName), 확장자순(FileExt).. 정렬하고 싶은데



제가 아래처럼 했더니 에러가 나더군요..

function FileListSort(Item1, Item2: Pointer): Integer;

begin

if (TFileList(Item1).FileName) < (TFileList(Item2).FileName) then

Result := -1

else begin

if(TFileList(Item1).FileName) > (TFileList(Item2).FileName) then

Result := 1

else

Result := 0;

end;

end;



참고로.. TFileList는

TFileList = Record

FileName: TFileName; //파일명

FileExt: String[3+1]; //확장자

Size: Integer; //파일크기

Attrib: Integer;

IsSelect: Boolean;

end;



TList클래스에 Sort함수로 정렬 방법에 아시는분 자세히

좀 가르쳐 주심 감사하겠습니다. ^^;



그럼..이만.

1  COMMENTS
  • Profile
    글쎄요 1999.11.19 14:24
    강민주 wrote:

    > 안녕하세요.

    > TList클래스를 이용해 파일리스트를 만들어

    > 화면에 출력할려고 하는데..

    > 이걸.. 이름순(FileName), 확장자순(FileExt).. 정렬하고 싶은데

    >

    > 제가 아래처럼 했더니 에러가 나더군요..

    > function FileListSort(Item1, Item2: Pointer): Integer;

    > begin

    > if (TFileList(Item1).FileName) < (TFileList(Item2).FileName) then

    > Result := -1

    > else begin

    > if(TFileList(Item1).FileName) > (TFileList(Item2).FileName) then

    > Result := 1

    > else

    > Result := 0;

    > end;

    > end;

    >

    > 참고로.. TFileList는

    > TFileList = Record

    > FileName: TFileName; //파일명

    > FileExt: String[3+1]; //확장자

    > Size: Integer; //파일크기

    > Attrib: Integer;

    > IsSelect: Boolean;

    > end;

    >

    > TList클래스에 Sort함수로 정렬 방법에 아시는분 자세히

    > 좀 가르쳐 주심 감사하겠습니다. ^^;

    >

    > 그럼..이만.

    ////

    다음을 참고하셔요. 그럼....

    Chad Rasque wrote in message ...

    >I'm having trouble understanding the procedural type parameter (Compare:

    >TListSortCompare) to pass to the TList.Sort method. I have populated a

    >TList with Record Pointers for the following record layout:

    >

    >SortList: TList;

    >TSearchTabRec = Record

    > LineNo: Integer;

    > FindText: String;

    > end;

    > PSearchTabRec = ^TSearchTabRec;

    >

    >I simply want to sort based on the the entire Record definition (i.e. Both

    >fields LineNo and FindText). If anyone can tell me what to pass to the

    Sort

    >method, I'd really appreciate. I can't find an example in any of the

    Delphi

    >Books I referenced.

    >

    >Thanks!

    >



    Chad,



    Create a function with this format (note: it has to be a function, not a

    method of an object):



    function SortMyRecords(Lhs, Rhs : Pointer) : Integer;

    const

    cmpIsLess = -1;

    cmpIsGreater = 1;

    begin

    // Assumption: LineNo is the primary sort key and FindText is the

    secondary sort key.

    // To change this behavior, check FindText first, then check LineNo.



    if TSearchTabRec(Lhs).LineNo < TSearchTabRec(Rhs).LineNo then

    Result := cmpIsLess

    else if TSearchTabRec(Lhs).LineNo > TSearchTabRec(Rhs).LineNo then

    Result := cmpIsGreater

    else

    // LineNo properties are equal, so check FindText.

    Result := AnsiCompareText(TSearchTabRec(Lhs).FindText,

    TSearchTabRec(Rhs).FindText);

    end;



    To use this function:



    MyList.Sort(SortMyRecords);



    That's it.