Nero Young
Nero Young

Reputation: 111

How to alphabetically sort code in XML file inside Intellij IDEA 13 for android?

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

Answers (4)

User Rebo
User Rebo

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.

  • For Android there is a plugin called AndroidXMLSorter (can only sort by attribute 'name', only in Android Projects)
  • If the entries are written in a single line you can select the text and use: Edit → Sort Lines (no need for a plugin)

Upvotes: 0

Darryl Staflund
Darryl Staflund

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:

  1. Open the Tools menu and make sure that 'Vim Emulator' is checked.
  2. Type the following command in your editor:

    :3,6sort

  3. 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

user3956566
user3956566

Reputation:

I found this link Rearrange Attributes in Android XML Files with IntelliJ IDEA 13

how to arrange xml

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:

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

xcel reorder

Upvotes: 3

Javaru
Javaru

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

Related Questions