SHCIDS_CANONICALONLY is the moral equivalent in the shell namespace of the Unicode ordinal comparison


One of the flags you can pass to the IShellFolder::CompareIDs method is SHCIDS_CANONICALONLY. This flag means that the method should determine whether the two pointers refer to the same underlying object, and if they do not, then it should determine which one should come first by whatever mechanism it wants. It doesn't matter which one is declared as coming before the other one, as long as it is consistent.

I like to think of this as the moral equivalent of the Unicode ordinal comparison. In both cases, you use the comparison if you have two items that you wish to keep in sorted order, but you don't care what the ordering rules are, as long as they are consistent. In fact, all you care about is consistency, and you're perfectly happy to sacrifice readability for speed. The resulting sorted list won't be displayed to the user; all you're going to use it for is locating the item later.

You can think of this as the moral equivalent of the NTFS file name sorting algorithm. In both cases, the items are sorted not so that the user can find them, but so that the program can find them.

Comments (3)
  1. Pierre B. says:

    This reminds me of a project I’ve worked on. They use names for classes, functions and variables that are technically and semantically correct but are undecipherable for mere mortals.

    I mean, SHCIDS_PRETTY_MUCH_LIKE_MEMCMP sunds clearer to me :). (YMMV, others may find even better, simpler names.)

  2. MadQ says:

    Wiser people than me might have more to say on the subject, but the behavior of SHCIDS_CANONICALONLY is absolutely correct for in-memory strings. However, you might run into trouble in sometimes, because some Unicode characters have multiple binary representations, so the strings should be normalized before they are compared ordinally. The .NET framework is really good at getting this right. Unfortunately, the .NET framework has so many different ways and options for string comparisons that a lot of source code I’ve seen gets it all wrong. The most common case being comparisons using the current thread’s cultural rules.

  3. Joshua Muskovitz says:

    But is the consistency consistent across multiple machines? Will the same list sort in the same order everywhere? Or is it local-system-only consistency?

Comments are closed.