Sublime Forum

Sort Lines By Column


A plugin for sorting lines of columnar data based on whichever column the user has selected with the cursor. Basically you highlight all the rows you want to sort, and then place the end cursor on the column to use as a key. Mostly just a re-implementation of the sort command line tool, but I find it comes in handy enough for things like sorting import *, etc.

I didn’t see any mentions when I did a search for sort columns, so I hope this isn’t reinventing the wheel.

import sublime
import sublime_plugin

# Don't want a global but the sort key won't take colkey as a parameter
colkey = -1

def colsort(item):
    global colkey

    # Special case where the item is a blank line in otherwise normal col data
    if len(item) < colkey + 1:
        return float("inf")
    return item[colkey]

class SortLinesColumnCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        global colkey

        for selection in self.view.sel():
            if(selection == None):

            # get region of word under cursor and that line
            wordselection = self.view.word(selection.end())
            last_line_sel = self.view.line(selection.end())

            # Get str of the beginning of last line up to cursor
            line_start = self.view.substr(sublime.Region(last_line_sel.begin(),
            # column that the end cursor is positioned at
            colkey = len(line_start.split(" ")) - 1

            # position of the start of the line at the beginning of selection
            startp = self.view.line(selection.begin()).begin()

            # create array of all lines in the selection region
            sortarray = ]
            for linesel in self.view.lines(selection):
                sortarray.append(self.view.substr(linesel).split(' '))

            # erase the lines from the window so we can place in proper order
            for linesel in reversed(self.view.lines(selection)):
                self.view.erase(edit, self.view.full_line(linesel))

            # sort then append the list together as a single string with \n
            sorted_str = ""
            for line in sortarray:
                sorted_str += " ".join(line) + "\n"  # Need space between cols

            # paste the sorted string onto the window
            self.view.insert(edit, startp,  sorted_str)
1 Like


Ooh this is great! Would you be willing to pack it up into something installable through Package Control?
Thanks! :smile:



[quote=“adzenith”]Ooh this is great! Would you be willing to pack it up into something installable through Package Control?
Thanks! :smile:[/quote] … /pull/1011



I would really like this, but it’s not showing up in package manager for me (sublime text v3). I get the numeric sort plugin, but not this. Am I missing something?



I installed the plugin with Package Manager. How do I actually use it?


#6 is not specified to work for ST3 on the package control channel, which is why you won’t be able to find it on ST3. @Kentzo, you might want to change that.

For other needs, there is also … 0Selection .

1 Like