TStringList에는 Sort기능이 있다
이것은 프로그램에 매우 유용하게 쓰일수 있다.
TStringList의 Sort기능 QuickSort로 구현되어있다.
아래팁에 TStringList의 VCL소스를 참조하라
http://cbuilder.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_tip&no=733
아래 샘플들은 TMemo 에 있는 내용을 TStringList를 Sorting하는 예제들이다.
[방법1]
;기본 Sort() 메소드를 이용
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TStringList *lst=new TStringList;
lst->Assign(Memo1->Lines);
lst->Sort();
Memo1->Lines->Assign(lst);
delete lst;
}
[방법2]
;Sorted = true 기능 이용
; 이 기능은 특별히 Sort함수를 호출하지 않아도
; data가 add될때 알아서 Sort해준다.
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TStringList *lst=new TStringList;
lst->Sorted=true;
lst->Assign(Memo1->Lines);
Memo1->Lines->Assign(lst);
delete lst;
}
[방법3]
; 정렬하면서 중복되는 데이타 삭제
; 방법2의 기능에서 list의 Duplicates=dupIgnore 로 해주면 중복되는 데이타는 삭제된다.
void __fastcall TForm1::Button3Click(TObject *Sender)
{
TStringList *lst=new TStringList;
lst->Sorted=true;
lst->Duplicates=dupIgnore;
lst->Assign(Memo1->Lines);
lst->Add("dfaf");
Memo1->Lines->Assign(lst);
delete lst;
}
Duplicates 옵션은 3가지가 있다.
0- dupIgnore : 중복되는 데이타 무시
1 - dupError : 중복인 경우 에러발생
2 - dupAccept : 중복인경우에도 수용
[방법4]
; 그런데 StringList의 Sort기능은 기본적으로 string을 compare한다.
즉 1,2,3,14,23,31 이 있는경우 정렬하면 1,14,2,23,3,31 이런 순으로 정렬된다.
오름차순 순서대로 1,2,3,14,23,31 순으로 정렬하기 위해서는 CustomSort 기능을 이용하여
직접 Sort함수를 만들어서 쓰면된다.
int __fastcall ListSortIntegerProc(TStringList *lst,int r1,int r2)
{
if(StrToInt(lst->Strings[r1])>StrToInt(lst->Strings[r2]))return 1;
else if(StrToInt(lst->Strings[r1])Strings[r2])) return -1;
else return 0;
}
void __fastcall TForm1::Button5Click(TObject *Sender)
{
TStringList *lst=new TStringList;
lst->Assign(Memo1->Lines);
lst->CustomSort(ListSortIntegerProc);
Memo1->Lines->Assign(lst);
delete lst;
}
//------------------------------------------
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // 델파이 //오름차순 정렬 function ListSortIntegerProcAsc(lst:TStringList;r1,r2:Integer):Integer; begin if(StrToInt(lst.Strings[r1])>StrToInt(lst.Strings[r2])) then Result:= 1 else if(StrToInt(lst.Strings[r1])(LST.Strings[r2]))then Result:=-1 else Result:= 0; end; procedure TfrmWebHard.Button2Click(Sender: TObject); var sLst: TStringList; begin sLst:=TStringList.Create; sLst.Assign(Memo1.Lines); sLst.CustomSort(ListSortIntegerProcAsc); Memo1.Lines.Assign(sLst); sLst.Free; end; |
[방법5]
; 방법4와 같은 방법인데 내림차순으로 정렬하고 싶으면
; CustomSort로 넘겨주는 process만 수정하면된다.
int __fastcall ListSortIntegerDescProc(TStringList *lst,int r1,int r2)
{
if(StrToInt(lst->Strings[r1])Strings[r2]))return 1;
else if(StrToInt(lst->Strings[r1])>StrToInt(lst->Strings[r2])) return -1;
else return 0;
}
//----------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
TStringList *lst=new TStringList;
lst->Assign(Memo1->Lines);
lst->CustomSort(ListSortIntegerDescProc);
Memo1->Lines->Assign(lst);
delete lst;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | //델파이 //내림차순 정렬 function ListSortIntegerProcDesc(lst:TStringList;r1,r2:Integer):Integer; begin if(StrToInt(lst.Strings[r1])>StrToInt(lst.Strings[r2]))then Result:= -1 else if(StrToInt(lst.Strings[r1])(LST.Strings[r2]))then Result:= 1 else Result:= 0; end; procedure TfrmWebHard.Button3Click(Sender: TObject); var sLst: TStringList; begin sLst:=TStringList.Create; sLst.Assign(Memo1.Lines); sLst.CustomSort(ListSortIntegerProcDesc); Memo1.Lines.Assign(sLst); sLst.Free; end; |
[방법6]
; 마지막으로 TStringList를 이용하여 Sort기능을 이용할때
sorting전에 순서는 기억하지 못한다.
sorting후에 sort전에 순서가 필요한 경우 다음과 같이 하면된다.
void __fastcall TForm1::Button7Click(TObject *Sender)
{
TStringList *lst=new TStringList;
lst->Assign(Memo1->Lines);
for(int i=0;iCount;i++)lst->Objects[i]=(TObject *)i;
lst->CustomSort(ListSortIntegerProc);
Memo1->Lines->Clear();
for(int i=0;iCount;i++)
Memo1->Lines->Add(lst->Strings[i]+" orginal order:"+IntToStr(int(lst->Objects[i])));
delete lst;
} 원본링크 : http://www.borlandforum.com/impboard/impboard.dll?action=read&db=bcb_tip&no=735
'공부방 > Delphi' 카테고리의 다른 글
| [델파이] 문자에서 숫자만 추출 (1) | 2014.08.19 |
|---|---|
| [델파이] 주민번호, 카드번호 체크 함수 (0) | 2014.08.14 |
| [델파이] PostMessage 와 SendMessage (1) | 2014.02.13 |
| [델파이] 엑셀 제어 (0) | 2013.12.31 |
| [델파이] 변수에 대해서 (0) | 2013.11.13 |