It's a common practice in Unity to start the name of a scene, prefab, etc. with an underscore so it will be listed first in the project window.

This is because things are sorted lexicographically and underscores come before letters in ASCII, right?

Well, no.

asciitable.com/

So what the heck is Unity using to sort things? Did they ... did they really add the underscore as a special case?

@peterdrake Sorting by ASCII order would be terrible anyway because then upper and lower cases of the same letter would end up in totally different sections in the results. Any competent alphabetic search would split and sort between letters, numbers, and punctuation first.

Also, I have not heard of using the underscore that way. That sounds like a sign that you need to refactor instead of hacking stuff in. I always name things based on their form/function. If I prefix it with anything, it's the type (i.e. "MatSteel")

@LouisIngenthron @andykorth

"Sorting by ASCII order would be terrible anyway because then upper and lower cases of the same letter would end up in totally different sections in the results. Any competent alphabetic search would split and sort between letters, numbers, and punctuation first."

I disagree. In theory, all items in a given directory would be capitalized the same way, so this shouldn't matter, right?

More importantly, everybody else does it by ASCII. For example, Python:

sorted(["_b", "B", "a", "_a", "c", "C", "_"])
['B', 'C', '_', '_a', '_b', 'a', 'c']

For the problem I'm running into, apparently Pixel Crushers' Dialogue System sorts the quests by ASCII, not the Unity/C# way. When I wanted certain quests to show up first, I put underscores at the beginnings of those quests behind-the-scenes names. I had assumed incorrectly (based on the aforementioned behavior) that the underscore came before letters. Now I have to repeat that extensive surgery.

"I have not heard of using the underscore that way."

I believe Bond uses it in his (excellent) book. Also, for example:

forum.unity.com/threads/editor

Finally, I can't find where this surprising sorting behavior is documented, either for Unity or C#.

Follow

@peterdrake @andykorth Maybe I've just gotten used to the .NET/Windows way. 🤷‍♂️

Here's some more info about how it works behind the scenes: learn.microsoft.com/en-us/dotn

@LouisIngenthron @andykorth Thanks.

I can't imagine how I would have ever found that page, even if I was looking for it.

@peterdrake @andykorth I went to the String.CompareTo page first, and it led me there.

Sign in to participate in the conversation
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.