quarks
quarks

Reputation: 35282

Automatically sort member variables alphabetically

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?

Upvotes: 13

Views: 8604

Answers (2)

user10639668
user10639668

Reputation:

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.

Upvotes: 16

quarks
quarks

Reputation: 35282

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

Upvotes: 0

Related Questions