Bridge Plus question

(Ed Stockly) #1
set sortedList to theLib's sublistsIn:listOfLists sortedByIndexes:{2, 1} ascending:{true, true} sortTypes:{"compare:", "compare:"}

This command is almost perfect, but is it possible to make it sort without being case sensitive?

I tried adding

caseSensitive:{false, false}

But that didn’t help

(Shane Stanley) #2

For text, you should generally use localizedCompare: rather than compare: as the sort selector. if you want to match the Finder’s sorting, for file names, use localizedStandardCompare:.

(if you want the complete details of what the various text comparison methods do, ask @NigelGarvey. It makes my head hurt.)

(Ed Stockly) #3

Hey, @NigelGarvey, what do the various text comparison methods do?

(I have aspirin on hand, just in case)

(Nigel Garvey) #4

Well. Not to give away too many trade secrets, a good way to find out is to write a test script and see what happens. :wink:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"

(* Tests with punctuation, digits, and Latin characters. Results best viewed in Script Debugger's "Desc" mode. *)

set anArray to current application's class "NSArray"'s arrayWithArray:({"baRk", "banana", "banäl", "bananA", "azure", "012", "calumny", "Aardvark", "ba_k", "_12", "-012", "Azure", "2", "11", "banana", "(banana)", "{banana}", "[banana]", "Banäna", "aardvark", "cat", "banäna", "Aardvark", "brickbat", "bark", "Banana", "banana", "Calumny", "Aardvark"})

anArray's sortedArrayUsingSelector:("compare:")
return result
--> Sorted by Unicode number. ("(" < "-" < digits < upper < "[" < "_" < lower < "{".)

anArray's sortedArrayUsingSelector:("caseInsensitiveCompare:")
--return result
--> Simile, but case-insensitively. Diacriticals still considered. ("(" < "-" < digits < "[" < "_" < letters < "{".)
-- Where strings are identical apart from case, it's apparent that the sort is stable.

anArray's sortedArrayUsingSelector:("localizedCompare:")
--return result
--> (en_GB) Case-insensitive and diacriticals ignored! Punctuation < digits. ("_" < "-" "(" < "[" < "{" < digits < letters.)
-- Where strings are identical apart from case or diacriticals, diacriticals are considered first, then lower < upper.

anArray's sortedArrayUsingSelector:("localizedCaseInsensitiveCompare:")
--return result
--> (en_GB) As "localisedCompare:" except that case is ignored in otherwise identical strings, which are sorted stably.

anArray's sortedArrayUsingSelector:("localizedStandardCompare:")
--return result
--> (en_GB) As "localizedCompare:" except that numeric sequences are sorted by the number values represented.

(* Test of sort stability generally. *)

set anotherArray to current application's class "NSMutableArray"'s new()
repeat with i from 1 to (count anArray)
	tell anotherArray to addObject:({originalIndex:i, sortingValue:(item i of anArray)}) -- This actually adds an NSDictionary. Otherwise:
	-- tell anotherArray to addObject:(current application's class "NSDictionary"'s dictionaryWithDictionary:({originalIndex:i, sortingValue:(item i of anArray)}))
end repeat

set sortingValueDescriptor to current application's class "NSSortDescriptor"'s sortDescriptorWithKey:("sortingValue") ascending:(true) selector:("compare:")
anotherArray's sortedArrayUsingDescriptors:({sortingValueDescriptor})
--> Whichever selector's used, the sort is stable. Dictionaries whose "sortingValue"s are judged by the selector to be identical remain in their original order with respect to each other.

(Ed Stockly) #5

localizedCaseInsensitiveCompare = Bingo! Thanks!