ItemSort Example
Keywords: ItemSort Example
In version 98F of Winbatch, we changed the way the following string sorting and comparison functions operate:BinarySort ItemSort ItemSortNc StrCmp StriCmp >, >=, <, and <= (operators)Previous to version 98F of Winbatch, these functions were sorting (or comparing) strings on using various different character-by-character algorithms. They now perform a "word sort", which sorts strings based on their collation sequence. Hyphens and apostrophes are considered of minor importance and sort close to the same word without the hyphen or apostrophe. All other non-alphanumeric characters are sorted before any alphanumeric character.The sorting sequence also more or less matches the sort sequence in AskItemList and AskFileName when the @SORTED parameter is elected. Note that those functions use a case-insensitive sort, so strings that differ only in case will show up in a random sequence.
This change may impact some scripts. After much discussion, it was decided it would be best to possibly break scripts and get the changes over with so at least it is standardized in the future. Note that the values for the underlying ANSI codes for the characters are not considered significant in the sort or compare process. Also note that the sequence may be different for different language versions of windows, as collating sequences differ between languages.
In the following ItemSort example, now case doesn't matter ...
itemstring="OD oD O ODonald O'Donald O" count=6 ;ITEMSORT sortstring=ItemSort(itemstring," ") ;Message(itemstring,sortstring) ; STANDARD < OPERATOR a1=ItemExtract(1,itemstring," ") a2=ItemExtract(2,itemstring," ") a3=ItemExtract(3,itemstring," ") a4=ItemExtract(4,itemstring," ") a5=ItemExtract(5,itemstring," ") a6=ItemExtract(6,itemstring," ") for xx=1 to count for yy=1 to count if a%xx% < a%yy% temp=a%xx% a%xx% = a%yy% a%yy% = temp endif next next newstr="" for xx=1 to count newstr=strcat(newstr,a%xx%," ") next ;STRCMP a1=ItemExtract(1,itemstring," ") a2=ItemExtract(2,itemstring," ") a3=ItemExtract(3,itemstring," ") a4=ItemExtract(4,itemstring," ") a5=ItemExtract(5,itemstring," ") a6=ItemExtract(6,itemstring," ") for xx=1 to count for yy=1 to count if strcmp(a%xx%,a%yy%)==-1 temp=a%xx% a%xx% = a%yy% a%yy% = temp endif next next newstr2="" for xx=1 to count newstr2=strcat(newstr2,a%xx%," ") next ;DISPLAY RESULTS results=strcat("ItemSort",@CRLF,sortstring,@CRLF,@CRLF,"< op",@CRLF,newstr,@CRLF,@CRLF,"strcmp",@CRLF,newstr2) Message(itemstring,results)In the example above the cases are intermingled. The sort order using ItemSort in versions of Winbatch prior to 98F sorted the items on a character-by-character string sort basis. Due to an upper management decision, in the 98F version of Winbatch, items are sorted on a word sort basis, just as Explorer or a dictionary would sort words. So we basically just reversed the sort order.Overview:
The sorting order changed in the 32-bit version of Windows, from a Win3.1 string sort to a Win95 word sort. At that time, when we discovered that ItemSort was sorting differently in the two versions of Windows, we decided to make them both sort in the same way, by forcing the sort to be a string sort.But upon later reflection, we decided a better way to standardize the 32-bit of 98F Winbatch, was to make it perform the same way that Explorer does, i.e., word sorting.
We also changed ItemSortNc so that, if the strings being sorted are the same on a case-insensitive basis, they will then be sorted on a case-sensitive basis.
To compare two strings case sensitively to determine whether two strings are equal, or which precedes the other in an ANSI sorting sequence, use StrCmp. (The relational operators >, >=, ==, !=, ≶=, and < provide the same capability.)
To compare two strings without regard to case, use StriCmp.
Additional FileItemize and Itemsort notes:
- FileItemize returns the names in the same order they are stored in the directory. Windows Explorer give you no way to see that particular order. I think the DOS DIR command might.
- ItemSort returns a "Dictionary" sort in which the sorted order is the same as the one you would get looking up the word in a standard dictionary. Numbers are not recognised as anything special. but treated like characters.
Article ID: W13093Filename: ItemSort and Other Sorting Functions.txt