Qsort,对stuct从大到小排序

 int  CompareFunction(const void *p1, const void *p2)

{
     return (((Wheel *)p1)->m_dProperty > ((Wheel *)p2)->m_dProperty) ? -1 : 1;
}

void SimpleGA::theWheelProperty()
{
    int wholeEnergy = theWholeEnergy();
    for(int i=0; i<globalVariable.m_nPopsize; i++){
        //printf("%lf ",((pOldPop+i)->getEnergy() / wholeEnergy));
        //对于求double的运算,一定要注意类型转化问题。
       (pWheel+i)->m_dProperty = (double) (pOldPop+i)->getEnergy() / wholeEnergy;
       (pWheel+i)->m_nPosition = i;
    }
    qsort(pWheel, globalVariable.m_nPopsize, sizeof(pWheel[0]), CompareFunction);

    for(int i=0; i<globalVariable.m_nPopsize; i++){
      std::cout<<(pWheel+i)->m_dProperty<<" ";
    }

    std::cout<<std::endl;
}

你可能感兴趣的:(职场,休闲,qsort,stuct)