Pavlo Dudchenko
Pavlo Dudchenko

Reputation: 41

How intellij idea sort imports by default?

I done some investigation, and got strange results. My classes:

Test.java:

package com.company;

import com.company.data.Byte;
import com.company.data.Class;
import com.company.zata.Long;
import com.company.zata.Short;
import com.company.data.*;
import com.company.zata.*;


public class Test {
    Object o1 = new Class();
    Object o2 = new SomeClass();
    Object o3 = new AppleClass();
    Object o4 = new Byte();
    Object o5 = new Long();
    Object o6 = new Short();
    Object o7 = new BetaClass();

}

All clases is simple, for test. For example one of all:

package com.company.data;

public class Class {
    public Class() {

    }
}

Tree of classes:

Tree of classes

I try organize imports for Intellij IDEA, Eclipse and Netbeans and got results:

Intellij IDEA before organize imports:

import com.company.data.Byte;
import com.company.data.Class;
import com.company.zata.Long;
import com.company.zata.Short;
import com.company.data.*;
import com.company.zata.*;

Intellij IDEA after organize imports:

import com.company.data.Byte;
import com.company.data.Class;
import com.company.data.*;
import com.company.zata.*;
import com.company.zata.Long;
import com.company.zata.Short;

Eclipse before organize imports:

import com.company.data.Byte;
import com.company.data.Class;
import com.company.zata.Long;
import com.company.zata.Short;
import com.company.data.*;
import com.company.zata.*;

Eclipse after organize imports:

import com.company.data.*;
import com.company.data.Byte;
import com.company.data.Class;
import com.company.zata.*;
import com.company.zata.Long;
import com.company.zata.Short;

NetBeans before organize imports:

import com.company.data.Byte;
import com.company.data.Class;
import com.company.zata.Long;
import com.company.zata.Short;
import com.company.data.*;
import com.company.zata.*;

NetBeans after organize imports:

import com.company.data.Byte;
import com.company.data.Class;
import com.company.data.*;
import com.company.zata.*;
import com.company.zata.Long;
import com.company.zata.Short;

Eclipse sorting imports alphabetical.

How sorting imports Intellij IDEA and NetBeans - I do not understand. I expected for Intellij IDEA and NetBeans this results:

import com.company.data.Byte;
import com.company.data.Class;
import com.company.data.*;
import com.company.zata.Long;
import com.company.zata.Short;
import com.company.zata.*;

Why Intellij Idea and Netbeans sort imports in this way?

Upvotes: 4

Views: 4808

Answers (3)

Pavlo Dudchenko
Pavlo Dudchenko

Reputation: 41

I got results with Intellij IDEA 15.0.1. If we try to organize imports with latest version Intellij IDEA, we got result:

import com.company.data.Byte;
import com.company.data.Class;
import com.company.data.*;
import com.company.zata.Long;
import com.company.zata.Short;
import com.company.zata.*;

I think it is correct sorting.

Upvotes: 0

Sweeper
Sweeper

Reputation: 273850

This is only a wild guess of mine.

What I saw is that IntelliJ and Netbeans try to move the imports as little as possible. See how the two .* imports stick together? I think that is because before it organized the imports, they stuck together as well.

The two editors see that if they just move the last two lines up and make the third and fourth lines replace the position of the last two lines, the package names will be sort in alphabetical order! So that's what they did.

Unfortunately, I cannot test this myself. But if you change the initial position of the .* imports, the result of organizing the imports might change.

Try organizing this:

import com.company.data.Byte;
import com.company.data.Class;
import com.company.zata.Long;
import com.company.data.*;
import com.company.zata.Short;
import com.company.zata.*;

If my guess is correct, the editor will swap the third and fourth lines.

Upvotes: 0

GhostCat
GhostCat

Reputation: 140613

The point is: why do you expect that different tools behave the very same way in the first place?!

You see, these different IDEs are using different rules that determine how imports are organized; and of course, those rules can be tweaked.

See this example how to make IntelliJ behave like eclipse; and over here for how to edit the Netbeans setup for this stuff.

Upvotes: 1

Related Questions