Class files sorting
In IDEA menu tree where all my class files are listed, I can sort my files by type or alphabetically.
That is nice. I need to sort alphabetically. But alphabetically sorting is not exactly alphabetically sorted. When in class name number characters are detected, it is sorted by number value, not as text.
For axample I have 3 classes Class_101_SomeClass, Class_1011_NothingSpecial, Class_901_Important.
I expect them to be in order Class_101_SomeClass, Class_1011_NothingSpecial, Class_901_Important.
But IDEA puts them according to number value in order Class_101_SomeClass, Class_901_Important, Class_1011_NothingSpecial.
I have put much effort in Eclipse to rename classes in that way that they stay in my required order. My Intention is to put some sorting code in beginning of file name followed by descriptive class name, and manage ordering in that way.
IDEA have broken all my logic. Now I only have option to rename to Class_1010_SomeClass, Class_1011_NothingSpecial, Class_9010_Important. But that is not a solution, since I do not know how much number characters I will ever need, and there is really many classes. I do not want to even start that.
Is there some option to tell IDEA not to detect number characters as number values, but treat them as ordinary characters in sorting?
Thank you!
Please sign in to leave a comment.
IntelliJ IDEA is used so called Natural Sort Order. See http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ for details.
There is no option to change this behavior.
Thank you for your answer.
_Numbering_ classes to impose order???
Somehow this reminds me of https://xkcd.com/1172/
(but this was not even a change in IDEA - it was always this way)
Nothing funny.
Not all of my classes are ordered that way.
In small packages alphabetical order does fine.
But some packages (especially large ones) need custom order.
For example in package where I store database tables definitions, I want that tables Persons, Addresses, BankAccounts stay together. Now I have T01_Persons, T011_Addresses, T012_BankAccounts.
And everything was fine - Eclipse and MySQL sorted my tables as I wish. Now I have switched to IDEA and logic is broken.
And what would be your solution without "spacebar heating" and going back to Eclipse?
Best regards,
Aigars
Sorry, I really did not want to mock you.
I just wanted to point out that whichever way you implement something, it will always leave part of you user base dissatified.
For this specific case:
I think most (as in "the majority") of users will appreciate that Class111 is sorted _after_ Class22.
(BTW that is also what recent Windows versions use in their Explorer.)
If you really want to name your classes to influence sort order in a specific tool like your IDE, then you'll have to rename them, e.g.
T010_Persons, T011_Addresses, ...
I think that will work in Eclipse, too.
Thank you, for your reply.
I understand that anyway someone will stay dissapointed.
I have thought about solution you suggest, but I think it will not work.
For example if after couple of months I decide to add table ZipCodes, I would like to put somewhere besides T011_Addresses. So Probably I should name it T0111_ZipCodes. One more digit in my sorting preffix, and I need to rename _ALL_ tables again.
That is realy painful, specialy if I want to integrate somehow with previous versions of my app, where tables had another names.
Same thing to Forms, DataModels. In my project all those are big packages with custom order, and I can not rename all of them everytime I need append new class.
I have not decided yet. Maybe I will convert sorting preffix to characters. That should work as I expect, and would be pain only for once.
Anyway thank you,
I was just hoping that there is little hack somewhere to make IDEA work as I have used to.
Best regards,
Aigars
So how about
T1_1_Persons, T1_2_Addresses, T1_3_BankAccounts.
?
If you want to order by groups, then inside each group, you'll need _two_ numbers, not one.
Interesting.
I have not thought about using some separating character inside numbers. Java ignores "_" inside number. In java 1_000 is the same as 1000. How about IDEA?
I have not previously thought about so called natural sorting order. Now I need to implement something that in IDEA (natural order) and MySQL (ascii order) bahaves same.
So what about natural sorting order (and particulary IDEA) how will be sorted those names:
T1_Persons, T2_Projects, T1_1_Addresses, T1_2_BankAccounts, T1_1_1_ZipCodes, T2_1_Category ?
Thank you!
Your best bet is to use a scheme where each prepended part is the same length.
So rather than having T1_X and T12_X, use T01_X and T12_X.
As 01 is both in natural and alphabetic ordering smaller than 12, it would solve the problem.
Everything is exactly as you say. Problem is that I do not know how many number characters I need. For example if I have sorted my items with two digit sorting preffix, there is no way to insert new item between T12 and T13.
In that case either I have to rename everything after T12 or create three digit sorting preffix, ang again rename everything.
More I think, more I believe that I will not use numbers for sorting anymore. In order to stay in compliance with natural sorting and ascii sorting, easiest way is to use characters for sorting.
Then I could be as flexible as I want, It is easy to insert item between AA and AB, just create item AAA.
As for my understanding there whould not be difference in natural and ascii sortings when using characters.
Thanks to everybody!