含汉字的字符串如何排序?
我用qsort + strcmp不行,比如"张"会拍到"貂"的前面
我看windows的listbox排序是正常的,我在想会不会有那个api调用,或者其他库会不会有,请问有没有现成的排序函数啊
[解决办法]
strcoll, wcscoll, _mbscoll
Compare strings using locale-specific information.
int strcoll( const char *string1, const char *string2 );
int wcscoll( const wchar_t *string1, const wchar_t *string2 );
int _mbscoll( const unsigned char *string1, const unsigned char *string2 );
Routine Required Header Compatibility
strcoll <string.h> ANSI, Win 95, Win NT
wcscoll <wchar.h>, <string.h> ANSI, Win 95, Win NT
_mbscoll <mbstring.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
Each of these functions returns a value indicating the relationship of string1 to string2, as follows.
Return Value Relationship of string1 to string2
< 0 string1 less than string2
0 string1 identical to string2
> 0 string1 greater than string2
Each of these functions returns _NLSCMPERROR on an error. To use _NLSCMPERROR, include either STRING.H or MBSTRING.H. wcscoll can fail if either string1 or string2 contains wide-character codes outside the domain of the collating sequence. When an error occurs, wcscoll may set errno to EINVAL. To check for an error on a call to wcscoll, set errno to 0 and then check errno after calling wcscoll.
Parameters
string1, string2
Null-terminated strings to compare
Remarks
Each of these functions performs a case-sensitive comparison of string1 and string2 according to the code page currently in use. These functions should be used only when there is a difference between the character set order and the lexicographic character order in the current code page and this difference is of interest for the string comparison.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tcscoll strcoll _mbscoll wcscoll
Locale Routines, String Manipulation Routines
[解决办法]
strcoll Functions Overview
See Also localeconv, _mbsnbcoll, setlocale, strcmp, _stricmp, strncmp, _strnicmp, strxfrm
#include <stdio.h>
#include <string.h>
#include <mbstring.h>
#include <locale.h>
int main() {
setlocale(LC_ALL,"chs");
printf("%d\n",strcmp("张","貂"));
printf("%d\n",wcscmp(L"张",L"貂"));
printf("%d\n",_mbscmp("张","貂"));
printf("%d\n",strcoll("张","貂"));
printf("%d\n",wcscoll(L"张",L"貂"));
printf("%d\n",_mbscoll("张","貂"));
return 0;
}
//-1
//-1
//-1
//1
//1
//1
//