请问TList的sort排序方法怎么用啊?
前天看borland 的帮助文挡,发现TList原来有排序的功能的,但文挡写得太简单了,可能也是小弟的水平有限,怎么也用不了。请各位指教. 问题点数:20、回复次数:7Top
1 楼chenjie100(OCEAN)回复于 2002-01-13 14:22:29 得分 0
其实排序功能源自于TStrings类。Top
2 楼Wingsun(孙春阳)回复于 2002-01-13 14:27:09 得分 20
这样用的:
class TForm1 : public TForm
{
__published: // IDE-managed Components
.....
....
private: // User declarations
static int __fastcall SortList(void * Item1, void * Item2);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
....
....
....
void __fastcall TForm1::Button2Click(TObject *Sender)
{
TList * pList=new TList();
int * Va;
randomize();
for(int i=0;i<10;i++)
{
Va=new int;
*Va=rand();
pList->Add(Va);
}
for(int i=0;i<10;i++)
{
ListBox1->Items->Add(IntToStr(*(int *)pList->Items[i]));
}
pList->Sort((TListSortCompare)SortList);
for(int i=0;i<10;i++)
{
ListBox2->Items->Add(IntToStr(*(int *)pList->Items[i]));
}
for(int i=0;i<10;i++)
{
Va=(int *)pList->Items[i];
delete Va;
}
pList->Clear();
delete pList;
}
//---------------------------------------------------------------------------
int __fastcall TForm1::SortList(void * Item1, void * Item2)
{
int a,b;
a=*(int*)Item1;
b=*(int*)Item2;
if(a>b)
return -1;
if(a==b)
return 0;
if(a<b)
return 1;
}
Top
3 楼Dala(Dala)回复于 2002-01-13 16:24:53 得分 0
hhxxTop
4 楼My_Love(浮萍)回复于 2002-01-13 17:08:32 得分 0
Dala(大拉):好久不见 Top
5 楼wildhorse01(ChinaBCB之雨中漫步)回复于 2002-01-13 20:59:32 得分 0
不错,不错,收藏Top
6 楼fscyber(无知无识)回复于 2002-01-14 14:48:51 得分 0
csdn果然高手众多Top
7 楼Dala(Dala)回复于 2002-01-15 08:21:13 得分 0
奇怪,M?_Lo?e(老?),你认识我吗?Top




