Reputation: 111
I used eclipse for training in android but now using Intellij IDEA 13. I am used to the graphical string editor of eclipse which can alphabetically sort the string.xml file for me. I want to do the same in intelliJ but couldn't find any solution. This is how code look like
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ActionBar App</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
</resources>
and I want to sort it alphabetically like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="action_settings">Settings</string>
<string name="app_name">ActionBar App</string>
<string name="hello_world">Hello world!</string>
</resources>
How to do this ? Please help!
Upvotes: 11
Views: 8447
Reputation: 4590
You can't sort XML elements by its attribute value, e.g. 'name' via File → Settings → Editor → CodeStyle → XML → Arrangement
. You only can sort the attribute order inside a tag or can sort the elements by its tag name.
Edit → Sort Lines
(no need for a plugin)Upvotes: 0
Reputation: 61
I am posting this two years after the fact, but there's another way to sort lines in IntelliJ IDEA that I'd like to mention here -- i.e. by using the IDE's Vim features.
If you want to sort a range of lines alphabetically -- say, lines 3 through 6 -- just do the following:
Type the following command in your editor:
:3,6sort
That's it -- your lines will now be sorted. All you need to do now is disable Vim if you don't use it. You can do this by Opening the Tools menu and unchecking 'Vim Emulator'.
Note that this isn't an XML sort -- it's an alphabetic line sort so spaces and so on could get in the way of a successful sort. But for simple cases like the one you mention above, this should work.
Darryl
Upvotes: 1
Reputation:
I found this link Rearrange Attributes in Android XML Files with IntelliJ IDEA 13
I chose the second option.
As mentioned go to the settings/Editor/Code Style/XML/Arrangement
Reordering the arrangement and changing to be reorder by name:
This won't sort the strings.xml
by name, so
copy and paste into an excel worksheet and sort by A-Z
Upvotes: 3
Reputation: 31936
The arrangement feature allows you to setup rules to arrange the order this appear in a file. Arrangement of attributes, particularly for Android files, was add via IDEA-72907. You can get a predefined arrangement style for android by going to Settings > [Project Settings] > Code Style > XML {Arrangement Tab} Click on the "Set from..." link near the top right and select Preferred Style > Android. That will populate the XML style (and arrangement) settings with a predefined style that you can tweak.
That said, I am unsure if the arrangement feature has the ability to sort by the attribute's value. I know it can sort the attributes in a particular order. If I get a chance, I'll see if I can find the syntax to sort by the attribute's value. In the meantime, you can take a look.
Also, you can try either the Lines Sorter or SortSelection third party plug-ins. A bit more basic, but one might work for you.
Upvotes: 3