10

I have a class:

public class Alphabetical {
   private String banana;
   private String apple;
   private String donut;
   private String zebra;
   private String carrot;
}

I want to automatically arrange the entries from A to Z (alphabetically)

I have tried https://blog.jetbrains.com/idea/2012/10/arrange-your-code-automatically-with-intellij-idea-12/ but this does not work, nothing happens when I press the Code > Rearrange Code menu. I am using IntelliJ 2018.2

And if this is not doable from IntelliJ, is there a maven plugin that can do this?

quarks
  • 33,478
  • 73
  • 290
  • 513

2 Answers2

14

Actually it is possible in Intellij. It doesn't work because the order-by-name is not enabled by default.

Go to Settings->Editor->Code Style->Java then click on Arrangement tab:

enter image description here

Then edit the 17th rule (the one with field private) and select in the combobox Order select order by name enter image description here

Now click Apply button and try again to rearrange fields.

  • Of course it is not the only place where it is applicable. For instance, ordering alpha. is also applicable for 'private static final' fields but it is not the line 17 but the line 4. The idea is to change the rules where it is desired. – Rudy Vissers Apr 28 '21 at 09:11
-1

My solution was to install Eclipse, load the same project and do

select the class, Source > Sort Members

based on Eclipse organize methods in alphabetical order

quarks
  • 33,478
  • 73
  • 290
  • 513