Error when comparing special charracters like "č"

Hello,

how can I compare strings, so the function will return special characters, such as "č" to be between "c" and "d" ? I tried to use strcmp and _mbsncmp, but the result is always the same

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int CALLBACK ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	char cItem1[TEXT_SIZE_MAX], cItem2[TEXT_SIZE_MAX];

	ListView_GetItemText(Selected.hWnd, lParam1, lParamSort, cItem1, TEXT_SIZE_MAX);
	ListView_GetItemText(Selected.hWnd, lParam2, lParamSort, cItem2, TEXT_SIZE_MAX);

	unsigned char cItem1w[TEXT_SIZE_MAX], cItem2w[TEXT_SIZE_MAX];
	memcpy(&cItem1w, cItem1, strlen(cItem1));
	memcpy(&cItem2w, cItem2, strlen(cItem2));
	
	/*if(iSortType == 0)
		return strcmp(cItem1, cItem2);
	else
		return (strcmp(cItem1, cItem2)*-1);*/
	if(iSortType == 0)
		return _mbsncmp(cItem1w, cItem2w, sizeof(cItem1));
	else
		return _mbsncmp(cItem1w, cItem2w, sizeof(cItem1))*-1;
}


projet: https://mega.co.nz/#!O8ZFXS7I!pYEwcdM7lTE7vktStZZhLENpY-e2VV7PszohY5QPBZA

screen: http://filebeam.com/2e3de1b87fa6c8f36fb2b03fdf94010e.jpg

the name in green rectangle should be up, after "C" and before "D", but it is at very last position

if u're going to actually compile the project, here's how oyu load CSV:

1. import
2. select "roc_projekt"
3. under "separator" use ";"
4. check the box
5. import
6. click on the second column, select "zoradit", "vzostupne"
closed account (2UD8vCM9)
You might be in unicode? Just a guess

If you're using visual studio
Go to Project -> Property tab at bottom

Click General tab
Under Character Set, change it to Multibyte
im using multibyte...
anyone?
Which codepage your system use? The problem probably is that codepages descending from ASCII places letters corresponding to latin ones to the lower area 0-127 and language specific ones into higher 128-255. That means latin letter will be before language specific ones.

You should look into boost::lexicographical_compare, or use C++ strings, C++ locales and std::lexicographical_compare
http://www.boost.org/doc/libs/1_42_0/doc/html/boost/algorithm/lexicographical_compare.html
_mbsncmp does the lexicographically compare.

See:
http://msdn.microsoft.com/en-us/library/aa272987%28v=vs.60%29.aspx

What you neet to do (at the beginning of the program):

setlocale(LC_ALL, "");

See:
http://msdn.microsoft.com/en-us/library/aa272906%28v=vs.60%29.aspx

otherwise it uses the "C" locale which is basically ASCII
@MiiNiPaa

my system, as OS? well, Slovak, which uses these letters (ľ,š,č,ť,ž,ý etc), as of application, Im using multi-byte. Also the compare function didnt work with strings if I remember right

@coder777

how exactly should I use it and how exactly should I compare strings?

i'm comparing them now with

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int CALLBACK ListViewCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
	char cItem1[TEXT_SIZE_MAX], cItem2[TEXT_SIZE_MAX];

	ListView_GetItemText(Selected.hWnd, lParam1, lParamSort, cItem1, TEXT_SIZE_MAX);
	ListView_GetItemText(Selected.hWnd, lParam2, lParamSort, cItem2, TEXT_SIZE_MAX);

	unsigned char cItem1w[TEXT_SIZE_MAX], cItem2w[TEXT_SIZE_MAX];
	memcpy(&cItem1w, cItem1, strlen(cItem1));
	memcpy(&cItem2w, cItem2, strlen(cItem2));
	
	if(iSortType == 0)
		return strcmp(cItem1, cItem2);
	else
		return (strcmp(cItem1, cItem2)*-1);
}


also i tried

setlocale(LC_ALL, NULL);

and

setlocale(LC_ALL, "slovak");

because of this: http://msdn.microsoft.com/en-us/library/aa246450(v=vs.60).aspx

but the result is still th same, I also tried both (NULL and slovak) with _mbsncmp, the result never changes
You should use

setlocale(LC_ALL, "");

This way it uses the current setting of your computer.

but the result is still th same, I also tried both (NULL and slovak) with _mbsncmp, the result never changes
ListView_GetItemText() results in your case in ANSI (not multibyte) so _mbsncmp() might not be appropriate. Other than I thought it probably doesn't use locale

strcmp() doesn't take the locale into accout either. Use strcoll:

http://www.cplusplus.com/reference/cstring/strcmp/
http://www.cplusplus.com/reference/cstring/strcoll/

great! thanks, works like a charm now
Topic archived. No new replies allowed.