Reputation: 2084
Is it possible to force IntelliJ to order imports alphabetically? So it should add import in lexicographical order and sort all imports that are not in good order when formatting the code.
Upvotes: 26
Views: 32873
Reputation: 41
If your issue is the order between the java and javax imports, then you can simply move it by clicking the Up and Down button on the Layout static imports separately
section.
For example, if we have this order:
Then if we do the Optimize Imports
, it will order the imports like this:
import java.util.Date;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
Upvotes: 0
Reputation: 9020
In case, none of the above answers work, try updating the ktlint
version and then running ./gradlew ktlint
. It was only solution for me that helped.
Upvotes: 0
Reputation: 26462
Reformatting code optimizes the imports by default, but it is also possible to invoke Code | Optimize Imports
separately.
You can configure the order of imports in:
Preferences/Settings | Editor | Code Style | Java
. Select the Imports
tab and look under the Import Layout
header at the bottom.
By default, imports are alphabetically ordered, except for the java.*
and javax.*
imports.
Upvotes: 24
Reputation: 181
I had the same problem. Building on what @bas-leijdekkers said that the default sort order is alphabetical, I was able to get my imports in the correct alphabetical order demanded by checkstyle, by doing this:
This worked for me, hope it helps you.
Upvotes: 5