xcsort - sort lines in Xcode

xcsort is an extension for Xcode to sort a text selection. This plugin integrates into the “Editor” menu and provides a new command “Sort Lines”. When you select several lines of text and use this command the lines are going to be sorted alphabetically.

This is useful to sort #include or #import lines. Imagine a code file that is often changed by a team of several people. It is quite common to get a conflict in a version control system. This conflict often happens in the #include lines block, because 2 people try to add a new #include line to the end of the block (on the same line). One way to reduce the number of conflicts is to keep the list of include lines sorted. It reduces the probability of several people editing the same line number. If a file has a lot of dependencies it can be tedious to keep it sorted. The xcsort plugin helps with the task of sorting #import lines in Xcode.

In addition to #include blocks the “xcsort” extension is useful to organize a set of constants, a block of repeating lines of code, or any lists of data in the text files. For example if NSNotification names are placed in a single file it could be beneficial to sort the lines. Another example is to sort text files with data like a list of categories in a JSON format.

The xcsort extension adds a feature to sort text in Xcode. While many other IDEs and text editors have this function out of the box, Xcode lacks an ability to sort lines by default. The Xcode version 8 introduced support for plugins which made this addition possible.

The plugin is available for download using the following buttons:


View on the App Store

Sort Lines Xcode command

How to use the extension?

  1. Open Xcode 8
  2. Open a project in Xcode
  3. Open a source code file or a text file
  4. Select several lines of text
  5. In the Xcode menu choose “Editor” - “xcsort” - “Sort Lines”

Requirements

The extension requires Xcode 8.
Xcode 8 requires at least macOS 10.11 El Capitan.

On some systems you need to do additional steps so that the extension appears in Xcode.

On macOS 10.12 Sierra and newer

  1. Open “System Preferences”, go to “Extensions”.
  2. Select “Xcode Source Editor” on the left side and enable “xcsort” extension.

Xcode extensions in System Preferences

On macOS 10.11 El Capitan:

  1. Run in a terminal: sudo /usr/libexec/xpccachectl
  2. Restart the computer