Jump to content

Natural Sort issue


Recommended Posts

Hi everybody 
Could you please confirm the Console display of this script on your computer ?

Local $r1 = _NaturalSort("15", "-15")  ; - 1
Local $r2 = _NaturalSort(".12", "15")  ; - 1
Local $r3 = _NaturalSort("-15", ".12") ; - 1

ConsoleWrite("$r1 = " & $r1 & "   $r2 = " & $r2 & "   $r3 = " & $r3 & @crlf)

Func _NaturalSort($sElem1, $sElem2)
    Return DllCall("shlwapi.dll", 'int', 'StrCmpLogicalW', 'wstr', $sElem1, 'wstr', $sElem2)[0]
EndFunc

Console display :

$r1 = -1   $r2 = -1   $r3 = -1

I got -1 three times, which means that :
* $r1 : "15"  has a lesser value than "-15" (msdn page)
* $r2 : ".12" has a lesser value than "15"
* $r3 : "-15" has a lesser value than ".12"

But something seems wrong because of the following incompatibility.
It will be sorted this way, when based on $r1 and $r3 results :
"15" < "-15" < ".12"

Or that way, based on $r2 and $r1 results :
".12" < "15" < "-15"

Or even like that, based on $r3 and $r2 results :
"-15" < ".12" < "15"

This could explain why I got "naturally sorted" rows placed differently, based on a same* initial array (same* means the same value of the elements, not considering their initial position in the unsorted array)

In my mind, no matter how the initial array is displayed before sorting, the result should be the same after the natural sort... but it's not : an array having its rows initially displayed as  "15", "-15", ".12" won't naturally sort same as if its rows were displayed as ".12" , "-15", "15" and the output will very often be different.

I'm aware this kind of values should be better treated using a numeric sort, but it's not a reason to ignore the potential problem caused by dots and minus signs during a natural sort.

There's a guy at this link who commented about this, at the very end of the web page :

It seems to be a misfeature of the "Natural sorting" function StrCmpLogicalW,
which sorts numbers by their value instead of sorting them althabetically,
e.g. 99 comes before 100. I guess it sees the "-" as a negative sign, and therefore ignores it.

Author of Total Commander
http://www.ghisler.com

But I'm not even sure this applies fully to the preceding issue (his comment about the minus sign)

So could you guys, please confirm that you got the Console display result "-1" three times ?
I hope you'll get a different result, it could mean that your newer version of "shlwapi.dll" has corrected this kind of discrepancy.

Especially when this note is written at the end of MS web page (msdn link above)

Note  Behavior of this function, and therefore the results it returns, can change from release to release. It should not be used for canonical sorting applications.

Thanks :)

Link to comment
Share on other sites

  • Moderators

pixelsearch,

$r1 = -1   $r2 = -1   $r3 = 1

3.3.16.0 on 64-bit Win 10 Hone 21H2.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

Great, that explains it all.

I was about to add a comment in my initial post, indicating that all values were unique (to eliminate the unstable/stable sort question) but it's not necessary now that you indicate that $r3 = 1 on your computer, which means a newer version of "shlwapi.dll" (this issue has nothing to do with AutoIt)

Ty M23 :thumbsup:

Edited by pixelsearch
M23, not MB23
Link to comment
Share on other sites

Thanks Exit for the test :)
Now that you and M23 confirmed that $r3 = 1 on your computers, It makes me wonder if the C++ update code I added yesterday in this link was really necessary :

"Added a 2nd way for Natural sorting (LarsJ's index creation adapted to C++)

Anyway it's done and I'll let it there, especially I'm not sure how C++ Sort native function (which is boolean) can correctly deal with StrCmpLogicalW() which returns 3 possible values -1, 0 or 1

And also because I added a personal stable sort part to LarsJ's code, in another C++ function. Who knows, it may be useful at times. But where is LarsJ, long time no see !
 

Edited by pixelsearch
typo
Link to comment
Share on other sites

Your issue has apparently been resolved.  
Nevertheless, here is the result from an older system environment :).

AutoIt = 3.3.14.0,  OS = WIN_7 Service Pack 1  CPU : X64  OS : X64

$r1 = -1   $r2 = -1   $r3 = 1

 

Musashi-C64.png

"In the beginning the Universe was created. This has made a lot of people very angry and been widely regarded as a bad move."

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...