Amiga CompareStr()

Report any Hollywood bugs here
Post Reply
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Amiga CompareStr()

Post by lazi »

This is ok:
CompareStr("Amiga","pc",False) /* -1 */

These two are wrong:
CompareStr("álma","alma",False) /* 0 */
CompareStr("á","a",False) /* 1 */

Should it work on Amiga (codesets.library) ?
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Amiga CompareStr()

Post by airsoftsoftwair »

Actually, this isn't wrong:

Code: Select all

CompareStr("á","a",False) /* 1 */
"á" is greater than "a" so this is right, but the other one is definitely wrong. It's fixed now. Unfortunately, codesets.library doesn't offer any collation capabilities so Hollywood uses a custom algorithm on Amiga/Linux/Android.

Code: Select all

- Fix [Amiga/Linux/Android]: Unicode sorting in CompareStr() and Sort() didn't work correctly for words
  which only differ in their diacritics 
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: Amiga CompareStr()

Post by lazi »

I wonder that why Sort and CompareStr uses different method of collation?
The 7.0 history said about this: - New: Sort() supports full Unicode collation now.

Edit:
Is it resolved by the fix you mentioned? Just realized that fix is not public yet.


Here are a simple test:

Code: Select all

SetFont(#SANS,20)

a={"abcd","ábcd","adcb"}
Sort(a)
for i,v in ipairs(a)
    nprint(i,v)
next

NPrint("")

Sort(a,CompareStr)
for i,v in ipairs(a)
    nprint(i,v)
next

WaitLeftMouse

The Sort(a) result, which is faulty:
ábcd
abcd
adcb


The Sort(a,CompareStr) result is correct:
abcd
adcb
ábcd

Other question is why drops me an "Invalid orderfunction for sorting" error when a fourth string is added to the array?
a={"abcd","ábcd","adcb","abce"}
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Amiga CompareStr()

Post by airsoftsoftwair »

lazi wrote: Sun Sep 29, 2019 9:06 pm I wonder that why Sort and CompareStr uses different method of collation?
No, they don't use different methods of collation so looks there is something wrong...
lazi wrote: Sun Sep 29, 2019 9:06 pm Other question is why drops me an "Invalid orderfunction for sorting" error when a fourth string is added to the array?
a={"abcd","ábcd","adcb","abce"}
Yeah, there's definitely something wrong here. I'll investigate :)
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Amiga CompareStr()

Post by airsoftsoftwair »

Well, actually, this isn't a bug in Hollywood but a case of RTFM ;)

The documentation of Sort() says:
Starting with Hollywood 4.5, you can customize the sorting operation by using a custom sort callback. This function has to accept two parameters and it has to return if the first parameter should be inserted before the second one or not. This gives you great flexibility in setting up custom sort operations because you can compare arbitrary values and you can also customize the sorting order.
So your callback function is expected to return either TRUE or FALSE, but not [-1,0,1] as done by CompareStr(). So the code needs to be fixed like this:

Code: Select all

Sort(a, Function(a, b) Return(CompareStr(a,b) = -1) EndFunction)
Then both problems will be gone.
Post Reply