47

Hello is there a way to sort the string names alphabetically in strings.xml ? It should sort it like this

Before

 <string name="ccc">CText</string>
 <string name="aaa">AText</string>
 <string name="bbb">BText</string>

After

 <string name="aaa">AText</string>
 <string name="bbb">BText</string>
 <string name="ccc">CText</string>

I am using Android Studio 1.5.1

2

8 Answers 8

69

I used AndroidXmlSorter plugin and it works perfectly for me.

enter image description here

How to install: Go to Android Studio -> Preferences -> Plugins and hit Browse repositories.. Search for AndroidXmlSorter, install and restart your Android Studio. Go to your strings.xml file and hit Ctrl+L. Voila.

Disclaimer: I'm not the author of this plugin, I just found it by chance and I think all the credits should go to the plugin author. The repo is here: https://github.com/roana0229/android-xml-sorter

10
  • 1
    Windows here. And seems like that project is abandoned anyways. Commented Nov 25, 2016 at 9:43
  • 1
    Just tried and works perfectly for me on Windows however the shortcut key mentioned doesn't work. I had to right click in my XML then Refactor -> Sort XML by Name. Commented Dec 20, 2017 at 13:42
  • 1
    Seem that the project is NOT abandoned. At the time of writing this plugin was updated a month ago. Works like a charm on Intellij 2018.2. Commented Aug 17, 2018 at 17:38
  • 1
    Works in June 2020 with Android Studio 4.0
    – user1114
    Commented Jun 2, 2020 at 17:27
  • 1
    Plugin will crash every time, if any comment inserted in xml file, just delete all comments Commented Sep 14, 2022 at 14:29
10

2022-09 | Simple plugin free solution:

Android studio includes option to sort lines / reverse lines.

  1. Highlight lines of code to be sorted unsorted lines

  2. Edit > sort lines or Edit > reverse lines sort line

  3. Enjoy (reverse) alphabetical order sorted lines

8

For anyone else who bumps into this. Copy all the string elements, paste into an Excel spreadsheet sort A-Z and then copy and paste back.

[Sort Android strings.xml in Alphabetical Order]

0
5

There is a plugin called Lines Sorter which can sort selected lines or whole files.

0

Sadly AndroidXmlSorter doesn't work for me when sorting multi-line strings.

My solution: Sort the strings.xml according to my android_strings_format.xslt (with xsltproc) and reformat it (with xmllint).

Cons: You have to execute a script. Therefore, it is not a true native feature of Android Studio. But you can easily execute scripts in Android Studio.

The script:

#!/bin/sh

sort_xml_file() {
  xsltproc --output "$1" android_strings_format.xslt "$1"
  export XMLLINT_INDENT="    "
  xmllint --format --encode "utf-8" --output "$1" "$1"
}

sort_xml_file "src/main/res/values/strings.xml"

The android_strings_format.xslt file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output 
    method="xml" 
    version="1.0" 
    encoding="UTF-8" 
    indent="yes" 
    omit-xml-declaration="yes"/>

<xsl:template match="/resources">
    <resources>

<xsl:for-each select="string">
<xsl:sort select="@name"/>
<string name="{@name}">
    <xsl:copy-of select="@* | node()"/>
</string>
</xsl:for-each>

</resources>
</xsl:template>

</xsl:stylesheet>

Works on Manjaro Linux x64.

EDIT: I have found a way to prevent multi-line text. I can enclose the text with quotes. <string name="available_version_error">"<u>ERRORa</u>"</string>

0

I used a XML formatter with an xslt definition - but this was too complex if you also uses plurals.

My solution: writing my own Python script:

import re

# add your files here
strings_files = ["path/to/strings.xml"]


# 1. transform the strings.xml file to a dict
# 2. sort the dict
# 3. transform the dict to a strings.xml file
def sort_strings_xml_file(path_to_file: str):
    entries = dict()
    current_entry_name = None

    # read entries from strings.xml file
    with open(path_to_file, "r") as file:
        for line in file.readlines():
            # a new entry was found in the strings.xml file
            if line.strip().startswith("<string ") or line.strip().startswith("<plurals "):
                current_entry_name = re.search(r'name="(.+)"', line).group(1)
                entries[current_entry_name] = ""

            # store content for the current entry
            if current_entry_name is not None:
                entries[current_entry_name] += line

            # stop recording for the current entry
            if line.strip().endswith("</string>") or line.strip().endswith("</plurals>"):
                current_entry_name = None

    entries = dict(sorted(entries.items()))

    # write results back to the strings.xml file
    with open(path_to_file, "w") as file:
        file.write(('<?xml version="1.0" encoding="utf-8"?>'
                    '<resources>'
                    f'{"".join(entries.values())}</resources>'))

    print(f"{path_to_file} was sorted")


for strings_file in strings_files:
    sort_strings_xml_file(strings_file)
-1

i would turn them to ASCII then sort that one and turn them back to string, very easy and very effective

-2

Update: Easiest way is: Ctrl+A then Ctrl+Alt+L

For configuration: In Android studio, you can quickly sort XML code by following step:

  1. Select all XML code in a file by Ctrl+A
  2. Use combination: Ctrl+Alt+Shift+L
  3. Select "Selected text" + "Rearrange code" Then Press RUN

View Image here

2
  • I don't know why but this doesn't work at my editor Android Studio 3.1.2.
    – jobbert
    Commented May 28, 2018 at 12:52
  • 1
    AFAIK, this only works for attributes in a single element. Not moving elements around.
    – JRomero
    Commented Sep 3, 2018 at 23:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.