View the latest, best formatted and most complete version of this manual online at http://mindprod.com/application/sortcode.manual.html.
|Comparators||Roll Your Own|
|Placing the Markers||Gotchas|
|Sorting Array Initialisations||Notes|
SortCode is a command line utility to alphabetically sort blocks of Java source code. You might use it to sort statements, enum constants, case blocks, static init code, array initialisation lines, or even blocks of text that have nothing to do with Java. For example, I use it to sort a free-form text file of information about vendors with a vendor heading, all in upper case separated by +++. It sorts the vendor blocks leaving the content of each block intact. I use it to sort products in HTML (Hypertext Markup Language) by price.You can either:
In either case, you must have some sort of delimiter between item. It might be a natural one, already there like , or ;. Failing that you have to insert them using strings such as +++, ___, /* */, // end method, // end class or <!-- +++ -->. A marker can be any string of character highly unlikely to appear naturally in the text and one that is ignored for other uses of the text.
SortCode will strip each item of leading and trailing blanks and arrange the blocks in your requested order and insert a blank line between them. It leaves your markers intact, ready for another sort.
Command line options after java.exe -jar sortcode.jar
|-marker="xxx"||error||marker that separates items. Ideally the last item has one after it too.|
|-start="xxx"||Sort the entire file||marker just in front of items to be sorted.|
|-end="xxx"||Sort the entire file||marker just in after the items to be sorted.|
|-comparator=xxx||Sensitive, a vanilla case-sensitive sort.||One of the following Comparators used to compare items for sorting. Note that the keyword -comparator and value are both case-sensitive. Pick one of Insensitive… SortPrices from the following list.|
|-v||terse||verbose — extra reporting on files not sorted or already sorted.|
|-q||terse||quiet — bare minimum of status reporting.|
|-s||No subdirs||Include subdirectories|
|file and directory names||Does nothing||Files and directories you want to process. All text files will be processed not just *.java.|
You can specify the parameters in any order. The only restriction is -s applies only to directories mentioned to the right of it.
I have built-in the following Comparators:
|Available SortCode Comparators|
|Insensitive||case-insensitive ascending alphabetic sort. (mixed upper and lower case together)|
|ReverseInsensitive||case-insensitive descending alphabetic sort. (mixed upper and lower case together)|
|ReverseSensitive||case-sensitive descending alphabetic sort. (lower case then upper case)|
|Sensitive||default. case-sensitive ascending alphabetic sort. (upper case then lower case)|
|Shuffle||scrambles the items into a different random order every time SortCode is run.|
|SortClasses||not yet working|
|SortDeclarations||Sorts declarations: public before private, static before instance, final before non-final, boolean, Boolean, char, Character, short, Short, int, Integer, long, Long, float, Float, double, Double, variable name case-insensitive ascending|
|SortMethods||Sorts methods: abstract before implemented, public before private, static before instance, native before non-native, boolean, Boolean, char, Character, short, Short, int, Integer, long, Long, float, Float, double, Double, method name case-insensitive ascending.|
|SortPrices||Finds a price of the form $1,345.49 or $1345.49 anywhere in the item. If there are none, it treats it as $0.00. If there are two, it uses the first one.|
Type these names precisely as shown. They are case-sensitive. It has to be this way because Java Comparator names are case-sensitive. You can also use any standard Java Comparator or write one yourself.
The markers are terminators, not separators. In other words, you should not put one before the first item and you should put one after the last item. SortCode is forgiving. If you add extra markers, or forget to add one at the end, it will repair your file automatically the next time it disturbs the sort order.
A typical and simple job for SortCode is sorting array initialisations. Extract just the initialisation lines you want to sort. Each line is terminated by a comma. But them in a file called extractedcode.javafrag. It might look like this:
then at the command prompt type:
The sorted results will look like this:
You might not like the double spacing. You can remove it with a search replace "\n\n" to "\n" or use the blout.exe with the -compact option. If your array initialisation items contained embedded commas, even if they were inside quotes, this procedure will not work. SortCode is quite simple minded.
To sort declarations in Java source code, you must first mark up your code with special comments to help SortCode find the declarations.
Just before all the declarations insert:
Just after all the declarations insert:
// end declarations
You don’t have to insert anything after each declaration. The natural ; at the end of each declaration acts as delimiter. Don’t include anything else, e.g. static init, instance init. A optional lead comment on each declaration is fine. You must put your keywords on each method in canonical order: public, protected, private, static, final, transient, volatile [int | long | String…]. To sort, type all on one line:
If you don’t like those markers, choose your own.
Unfortunately, any semicolons embedded in your strings will confuse it. I need a cleverer parser. It cannot deal with static init blocks.
SortDeclarations Comparator does not do a simple alphabetic sort. It assigns priorities to different keywords in a complex way then sorts by those priorities. If you don’t like the results, have a look at the source code for SortDeclarations and roll your own modification.
To sort methods in Java source code you must mark up your code with special comments to help SortCode find the methods.
Just before all the methods insert:
Just after all the methods insert:
// end methods
Just after each method insert:
// end method
Don’t include anything else, e.g. constructors, or enums or static inits, between the // methods and // end methods. You must put your keywords in canonical order: public, protected, private, abstract, static, final, synchronized, native, strictfp [ void | int | long | String… ]. To sort, type all on one line:
If you have HTML with prices in it of the form $1.49 you can sort the blocks in order by price. the code to do it is:
If there are multiple prices per block, it takes the first one. It ignores stray $ that are not prices. I this technique to sort headsets for sale by ascending price in HTML.
This comparator will shuffle the items. Every time you run it, it will shuffle items into a different random order. I don’t know a practical reason to do this. I was just showing off the generality of SortCode.
You can write your own Comparator<String> or use any you find on the net. You must specify its name fully qualified with the package. You can, of course, make up any delimiters you want. If you don’t specify the -start and -end markers, it will sort the whole file.
The com.abc.sortstuff.SortByDesirability.class custom Comparator class file must be available on the classpath. It will be dynamically loaded with Class.forName. You don’t have to specify the full package name to use the built-in Comparators. You might look at my Comparators and modify them for your purposes, or steal ideas from them how you might write your own. SortDeclarations and SortMethods do not sort in strictly alphabetical order. You will probably disagree with my ordering. You can easily modify it by writing your own Comparator very similar to mine.
Why the arrow icon? It represents ordering blocks A to Z. Since IntelliJ Rearranger no longer rearranges methods, we need something to sort Java declarations and methods by various categories and insert comments like the old Rearranger plug-in did. At this point SortCode is a very primitive declarations Rearranger. You have to insert all kinds of magic comments for it to work. If you don’t do that correct It will scramble your code. (Do a backup first.) If you don’t like the order, write your own Comparator based on SortDeclarations.
This a kludge, an emergency until IntelliJ Rearranger starts sorting again. With a real rearrange you can tidy your lifetime body of work with a couple of keystroke. With this primitive version you must separately extract declarations and methods or insert markup and methods from each class and process them, proofread the results and manually insert the results back in. However, it is faster and more accurate than doing it fully manually. I have on the back burner a parser project so you will not need to insert the magic comments in Java source code, though you will for other applications.
One of the nice features of the scheme is even if your custom Comparator is extremely buggy, all it will do is put your declarations and methods in some silly order. It won’t damage the data, just scramble the order of the items..
This page is posted
Optional Replicator mirror
Your face IP:[22.214.171.124]
You are visitor number|