13,825
社区成员
发帖
与我相关
我的任务
分享
int TMainForm::compare(AnsiString val1,AnsiString val2,int datetype)
{
int int1,int2;
double float1,float2;
int errcode;
if(datetype==0)
{ //文本比较
return AnsiCompareText(val1,val2);
}
else if(datetype==1)
{//整数比较
try
{
int1=val1.ToInt();
int2=val2.ToInt();
}
catch(...)
{
return 0;
}
if (int1>int2) return 1;
else if (int1<int2) return -1;
else return 0;
}
else if(datetype==2)
{//浮点数比较
try
{
float1=val1.ToDouble();
float2=val2.ToDouble();
}
catch(...)
{
return 0;
}
if (float1>float2) return 1;
else if (float1<float2) return -1;
else return 0;
}
return 0;
}
void TMainForm:: Quicksort(TStringGrid *Grid, int *List,int ListLen,int min, int max,int sortcol,int datatype)
{
int med_value ;
int hi, lo, i ;
if(min>=max) return;
randomize();
//随机取一个中值
i = min + random(max - min + 1);
med_value = List[i];
List[i] = List[min]; //{ Swap it to the front so we can find it easily}
//{Move the items smaller than this into the left
// half of the list. Move the others into the right}
lo = min;
hi = max;
while (1)
{
// Look down from hi for a value < med_value.
while( compare(Grid->Cells[sortcol][List[hi]],Grid->Cells[sortcol][med_value],datatype) >=0)
//(*ANSIComparetext(Grid.cells[sortcol,List[hi]]
// ,grid.cells[sortcol,med_value])>=0 do*)
{
hi--;
if (hi <= lo) break;
}
if (hi <= lo)
{// {We're done separating the items}
List[lo] = med_value;
break;
}
// Swap the lo and hi values.
List[lo] = List[hi];
lo++;// {Look up from lo for a value >= med_value}
while (compare(Grid->Cells[sortcol][List[lo]], Grid->Cells[sortcol][med_value],datatype)<0)
{
lo++;
if (lo >= hi) break;
}
if (lo >= hi)
{ // {We're done separating the items}
lo = hi;
List[hi] = med_value;
break;
}
List[hi] = List[lo];
}
//{Sort the two sublists}
Quicksort(Grid,List, ListLen,min, lo - 1,sortcol,datatype);
Quicksort(Grid,List,ListLen, lo + 1, max,sortcol,datatype);
}
void TMainForm::Sortgrid(TStringGrid *Grid,int sortcol)
{
int i ;
TStringGrid *tempgrid;
int *List; //需要排序的行的序号
int ListLen;
bool bTotalRow=false;
int datatype;//要排序数据的类型 0:按字符排序 1:按整型排序 2:按浮点型排序
if (!Grid) return ;
if (Grid->RowCount <= 1) return ;
if (Grid->RowCount == 2 && Grid->Cells[0][Grid->RowCount - 1].Trim().Length() == 0)
return ;
//判断是否有合计项目
for(int m=0;m<Grid->ColCount;m++)
{
if(Grid->Cells[m][Grid->RowCount-1].Pos("合计:")>0)
{
bTotalRow=true;
break;
}
}
Screen->Cursor=crHourGlass;
datatype=GetColType(Grid,sortcol,bTotalRow);//获取列的数据类型
//临时Grid,和原来的内容一致
tempgrid=new TStringGrid(Application);
tempgrid->RowCount=Grid->RowCount;
tempgrid->ColCount=Grid->ColCount;
tempgrid->FixedRows=Grid->FixedRows;
ListLen=tempgrid->RowCount-tempgrid->FixedRows-(bTotalRow?1:0);
List=new int[ListLen];
int c=bTotalRow?tempgrid->RowCount-1:tempgrid->RowCount;
for (i= 1;i<c;i++)
{
List[i-1]=i;
tempgrid->Rows[i]=Grid->Rows[i];
//TRACE("list[%d]:%d",i-1,i);
}
Quicksort(Grid, List,ListLen,0,Grid->RowCount-2-(bTotalRow?1:0),sortcol,datatype);
bool basc;
SetGridTitle(Grid, sortcol,basc);
if(basc)
{//顺序排列
c=Grid->RowCount-1-(bTotalRow?1:0);
for( i=0 ;i<c ;i++)
{
Grid->Rows[i+1]=tempgrid->Rows[List[i]];
}
}
else
{//逆序排列
c=Grid->RowCount-1-(bTotalRow?1:0);
for( i=0 ;i<c ;i++)
{
Grid->Rows[i+1]=tempgrid->Rows[List[tempgrid->RowCount-i-2-(bTotalRow?1:0)]];
}
}
Grid->Row=Grid->FixedRows;
tempgrid->RowCount=0;
delete tempgrid;
delete []List;
Screen->Cursor=crDefault;
}
int TMainForm::GetColType(TStringGrid * Grid, int sortcol, bool bTotalRow)
{
//判断1列的类型 0:按字符排序 1:按整型排序 2:按浮点型排序
//判断数据类型
bool bOK=true;
AnsiString str;
for(int m=Grid->FixedRows;m<(bTotalRow?Grid->RowCount-1:Grid->RowCount);m++)
{ //判断是否都是整数
str=Grid->Cells[sortcol][m].Trim();
try
{
str.ToInt();
}
catch(...)
{
bOK=false;
break;
}
}
if(bOK) return 1;
for(int m=Grid->FixedRows;m<(bTotalRow?Grid->RowCount-1:Grid->RowCount);m++)
{
//判断是否都是浮点数
str=Grid->Cells[sortcol][m].Trim();
try
{
str.ToDouble();
}
catch(...)
{
bOK=false;
break;
}
}
if(bOK) return 2;
else return 0;
}