我的列表响应了消息 LVN_COLUMNCLICK 对item 的内容进行降序排序,排列结果如下: 语言.txt
 疑问.txt
 最近学习.txt
 新建文本文档.txt
 已经修正的问题.txt
排序的算法如下:int MyListView::CompareFunction(LPARAM lParam1, LPARAM lParam2, LPARAM lParamData)
{
    MyListView* pListCtrl = reinterpret_cast<MyListView*>(lParamData);
    MyListItem* pItem1 = reinterpret_cast<KD_MyListItem*>( lParam1 );
    MyListItem* pItem2 = reinterpret_cast<KD_MyListItem*>( lParam2 );

    if (pListCtrl->m_iSortColumn == COLUMN_NAME)
    {
        CString str1(pItem1->GetListItemText(pListCtrl->m_iSortColumn));
        CString str2(pItem2->GetListItemText(pListCtrl->m_iSortColumn));

        if(pListCtrl->m_bAscSort)
            return str1.CompareNoCase(str2);
        else
            return str2.CompareNoCase(str1);
    }
}XP系统的列表(LVS_REPORT),对上面相同的文件名进行降序排序,排出的结果(如下), 最近学习.txt
 语言.txt
 已经修正的问题.txt
 疑问.txt
 新建文本文档.txt按照排列结果来看,文件是根据汉字的拼音来比较的,请问哪个大侠知道一些比较通用的比较算法?
或者贴一下有关汉字的排序算法。谢谢!

解决方案 »

  1.   

    我用ANSI版本测试了一下,排序没有问题,换成unicode版本,排序就不正常了。
    我想这个与字符编码也许有关系#include "stdafx.h"
    #include <tchar.h>
    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    using namespace std;int _tmain(int argc, _TCHAR* argv[])
    {
    char* data[] = {
    "语言",
    "疑问",
    "最近学习",
    "新建文本文档",
    "已经修正的问题",
    };

    vector<string> colTest;
    for (int i = 0; i < sizeof(data) / sizeof(data[0]); i++) {
    colTest.push_back(string(data[i]));
    }
    sort(colTest.begin(), colTest.end());
    copy(colTest.begin(), colTest.end(), ostream_iterator<string>(cout, "\n"));
    return 0;
    }
      

  2.   

    把unicode编码的字符串转化为ansi字符串,然后比较不就可以了么?
      

  3.   

    汉字排序,按拼音比较常见和合理,但你要有汉字拼音表如'WINPY.TXT',或把你用到的汉字转到拼音(确定的句子,又不多好办).
      

  4.   

    修正后的代码#ifdef _UNICODE
         int iSize = WideCharToMultiByte(CP_ACP, 0, str1, -1, NULL, 0, NULL, NULL); 
         char* pszMultiByte1 = new char[iSize+1]; 
         WideCharToMultiByte(CP_ACP, 0, str1, -1, pszMultiByte1, iSize, NULL, NULL);      iSize = WideCharToMultiByte(CP_ACP, 0, str2, -1, NULL, 0, NULL, NULL); 
         char* pszMultiByte2 = new char[iSize+1]; 
         WideCharToMultiByte(CP_ACP, 0, str2, -1, pszMultiByte2, iSize, NULL, NULL);      if(pListCtrl->m_bAscSort)
             return _stricmp(pszMultiByte1, pszMultiByte2);
         else
             return _stricmp(pszMultiByte2, pszMultiByte1);     delete [] pszMultiByte1;
         delete [] pszMultiByte2;
    #endif