ƒNƒ‰ƒX‚É”äŠr—pŠÖ”‚ðŽ‚¿ž‚ñ‚¾—á


#include "stdafx.h"
#include <iostream.h>
#include <afxtempl.h>
#include <afx.h>


class Test {
    int iId;
    CString sStr1;
    CString sStr2;
public:
    Test();
    ~Test();
    Test(int n, CString s1 ,CString s2);
    int getId();
    CString getStr1();
    CString getStr2();
    void display();

    static int __cdecl compareTestId(const void *pElem1, const void *pElem2);
    static int __cdecl compareTestStr1(const void *pElem1, const void *pElem2);
//  static int compareTestId(const void *pElem1, const void *pElem2);
//  static int compareTestStr1(const void *pElem1, const void *pElem2);

};

Test::Test() {
}

Test::~Test() {
}

Test::Test(int n, CString s1 ,CString s2) {
    iId = n;
    sStr1 = s1;
    sStr2 = s2;
}

int Test::getId() {
    return iId;
}

CString Test::getStr1() {
    return sStr1;
}

CString Test::getStr2() {
    return sStr2;
}

void Test::display() {
    cout << "Id=" << iId << " Str1=" << sStr1 << " Str2=" << sStr2 << endl;
}


// ID‚Å”äŠr
int Test::compareTestId(const void *pElem1, const void *pElem2) {
    Test **pTst1 = (Test**)pElem1;
    Test **pTst2 = (Test**)pElem2;
    int result = (*pTst1)->getId()  -  (*pTst2)->getId();
    return result;
}

// Str1‚Å”äŠr
int Test::compareTestStr1(const void *pElem1, const void *pElem2) {

    Test **pTst1 = (Test**)pElem1;
    Test **pTst2 = (Test**)pElem2;
    
    CString sTst1 = (*pTst1)->getStr1();
    CString sTst2 = (*pTst2)->getStr1();

    int result = sTst1.Compare(sTst2);

    return result;
}

void main() {

    CArray <Test*, Test*> aryTest;
    Test *pTest;
    int ix;
    
    pTest = new Test(3,"aaa","222");
    aryTest.Add(pTest);

    pTest = new Test(1,"bbb","555");
    aryTest.Add(pTest);
    
    pTest = new Test(5,"ccc","444");
    aryTest.Add(pTest);
    
    pTest = new Test(4,"ddd","111");
    aryTest.Add(pTest);
    
    pTest = new Test(2,"eee","333");
    aryTest.Add(pTest);
    
    Test **pSort;

    pSort = aryTest.GetData();


    /* Id‚Å•À‚בւ¦ */
    cout <<"Id‚Å•À‚בւ¦" << endl;
    
    qsort( pSort , aryTest.GetSize(), sizeof(Test*), Test::compareTestId );
    for ( ix = 0 ; ix < aryTest.GetSize() ; ix++ ) {
        pTest = aryTest.GetAt(ix);
        pTest->display();
    }


    /* Str1‚Å•À‚בւ¦ */
    cout << "Str1‚Å•À‚בւ¦" << endl;


    qsort( pSort , aryTest.GetSize(), sizeof(Test*), Test::compareTestStr1 );
    for ( ix = 0 ; ix < aryTest.GetSize() ; ix++ ) {
        pTest = aryTest.GetAt(ix);
        pTest->display();
    }
    

    for ( ix = 0 ; ix < aryTest.GetSize() ; ix++ ) {
        pTest = aryTest.GetAt(ix);
        delete pTest;
    }

    return;
}
ŽÀsŒ‹‰Ê

Id‚Å•À‚בւ¦
Id=1 Str1=bbb Str2=555
Id=2 Str1=eee Str2=333
Id=3 Str1=aaa Str2=222
Id=4 Str1=ddd Str2=111
Id=5 Str1=ccc Str2=444
Str1‚Å•À‚בւ¦
Id=3 Str1=aaa Str2=222
Id=1 Str1=bbb Str2=555
Id=5 Str1=ccc Str2=444
Id=4 Str1=ddd Str2=111
Id=2 Str1=eee Str2=333

–ß‚é