Вопрос: Нужно отсортировать имена файлов в FileListBox, как это сделать(по имени и расширению)?
Ответ :
code:
--------------------------------------------------------------------------------
// жми одну из трех кнопочек:
void __fastcall TForm1::Button1Click(TObject *Sender)
{//сортировка по имени в обратном порядке
String str="",buf="";
for (int j=0;j<FileListBox1->Items->Count-1;j++){
for (int i=0;i<FileListBox1->Items->Count-1;i++){
str=FileListBox1->Items->Strings[i];
buf=FileListBox1->Items->Strings[i+1];
if (str<buf){//если > то в прямом порядке
FileListBox1->Items->Move(i,i+1);
buf=str;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{//сортировка по имени в прямом порядке (2 способ)
TStringList *s=new TStringList;
s->Assign(FileListBox1->Items);
s->Sort();
FileListBox1->Items->Assign(s);
delete s;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{//сортировка по расширению
String str="",buf="";
for (int j=0;j<FileListBox1->Items->Count-1;j++){
for (int i=0;i<FileListBox1->Items->Count-1;i++){
str=FileListBox1->Items->Strings[i];
buf=FileListBox1->Items->Strings[i+1];
str=str.SubString(str.LastDelimiter("."),3);
buf=buf.SubString(buf.LastDelimiter("."),3);
if (str>buf){//знак - порядок сортировки
FileListBox1->Items->Move(i,i+1);
buf=str;
}
}
}
}
--------------------------------------------------------------------------------
(c) 2003 Borland X Portal
|