45

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

Murat Karagöz
  • 35,401
  • 16
  • 78
  • 107
  • See this blog post shared by Android Studio developers: http://blog.jetbrains.com/idea/2013/10/rearrange-attributes-in-android-xml-files-with-intellij-idea-13/ A lazy solution https://stackoverflow.com/a/13022926 – Ugurcan Yildirim Feb 23 '16 at 09:22
  • Sorry thats not what I am looking for. The rearrange attributes is the right way to go. Need to find out how to set the arrangement although. – Murat Karagöz Feb 23 '16 at 09:34

8 Answers8

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

L Y E S - C H I O U K H
  • 4,765
  • 8
  • 40
  • 57
nic
  • 2,125
  • 1
  • 18
  • 33
9

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]

Minion Jim
  • 1,239
  • 13
  • 30
Bajirao Shinde
  • 1,356
  • 1
  • 18
  • 26
5

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

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
5

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

GreenMarty
  • 232
  • 3
  • 7
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

Chief Madog
  • 1,738
  • 4
  • 28
  • 55
-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

Boken
  • 4,825
  • 10
  • 32
  • 42
Sy C Dang
  • 53
  • 3