Home Download Buy Blog Forum Support

Sort Lines By Column

Sort Lines By Column

Postby derekh on Mon Jan 14, 2013 9:17 am

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.
Code: Select all
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):
                continue

            # 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(),
                    wordselection.end()))
            # 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
            sortarray.sort(key=colsort)
            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)
derekh
 
Posts: 1
Joined: Mon Jan 14, 2013 9:07 am

Re: Sort Lines By Column

Postby adzenith on Tue Jan 15, 2013 1:57 am

Ooh this is great! Would you be willing to pack it up into something installable through Package Control?
Thanks! :)
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Sort Lines By Column

Postby Kentzo on Fri Jan 25, 2013 2:13 pm

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

https://github.com/wbond/package_contro ... /pull/1011
Kentzo
 
Posts: 4
Joined: Sun Jun 17, 2012 1:36 pm

Re: Sort Lines By Column

Postby Moz on Wed Dec 18, 2013 11:11 pm

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?
Moz
 
Posts: 1
Joined: Wed Dec 18, 2013 10:59 pm


Return to Plugin Development

Who is online

Users browsing this forum: No registered users and 11 guests