Sublime Forum

Sort Lines LengthWise

#1

Hi,
How to sort a file or a selected area according to length of lines in sublime.
I need to do it for css Properties
Like:
#topbar{
background-image:url(“images/abc.png”);
background-position: 12px 13px;
border-style: solid;
font-size: 1px;
left: 36px;
margin-top: 10px;
top: 23px;
vertical-align: center;
z-index: 1;
}

should convert into:

#topbar{
left: 36px;
top: 23px;
z-index: 1;
font-size: 1px;
margin-top: 10px;
border-style: solid;
vertical-align: center;
background-position: 12px 13px;
background-image:url(“images/abc.png”);
}

please, anyone can help me…

Thanks

0 Likes

#2

Not out of the box but very easy with a plugin.
Try to write it yourself and come back if you have troubles.

You will see that when you know how to write a plugin for ST2, you can’t stop even when it’s not really needed :wink:

0 Likes

#3

Hi all,
Actually I am very new to this sublime text and don’t know python. So it will be very hard for me to learn a bit of python then write this plugin. I have seen something about plugin coding for sublime but it’s all above my head.
So If anyone have this plugin please post it. I am needing it urgently.

Thanks

0 Likes

#4

You already ask for it one week ago, so I suppose it would be possible to spend some times to do it yourself and learn something new and useful :frowning:

]import sublime, sublime_plugin
import sort

def line_length_sort(txt):
    txt.sort(lambda a, b: cmp(len(a), len(b)))
    return txt

class SortLinesLengthCommand(sublime_plugin.TextCommand):
    def run(self, edit, reverse=False,
                        remove_duplicates=False):
        view = self.view

        sort.permute_lines(line_length_sort, view, edit)

        if reverse:
            sort.permute_lines(sort.reverse_list, view, edit)

        if remove_duplicates:
            sort.permute_lines(sort.uniquealise_list, view, edit)
0 Likes

#5

Thanks bizoo
It’s done …

0 Likes

#6

Thanks bizoo, very handy :smile:

0 Likes

#7

This unfortunately does not work anymore:

ModuleNotFoundError: No module named 'sort'

I have zero experience in creating plugins though, I might have done something trivially wrong.

I’d be grateful for any pointers.

0 Likes

#8

For anyone finding this thread, I ended up doing this with awk on the command line:

< testfile.txt awk '{ print length, $0 }' | sort -n -s | cut -d" " -f2-

0 Likes

#9

The reason that this doesn’t work is that the original plugin was written for Sublime Text 2. The sort module that it’s trying to do something with here is in the Default package now, so at a minimum that would have to become from Default import sort.

Also, versions of ST2 uses Python2 while ST3 and above use Python 3, so the call to the sort function of the array inside of line_length_sort is also no longer correct because it now requires you to specify the function to sort as a key keyword argument (and the function you pass gets only a single argument, not 2).

A working version would thus be:

import sublime
import sublime_plugin

from Default import sort

def line_length_sort(txt):
    txt.sort(key=len)
    return txt

class SortLinesLengthCommand(sublime_plugin.TextCommand):
    def run(self, edit, reverse=False, remove_duplicates=False):
        view = self.view

        sort.permute_lines(line_length_sort, view, edit)

        if reverse:
            sort.permute_lines(sort.reverse_list, view, edit)

        if remove_duplicates:
            sort.permute_lines(sort.uniquealise_list, view, edit)
1 Like