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:

[code]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)[/code]
0 Likes

#5

Thanks bizoo
Itā€™s done ā€¦

0 Likes

#6

Thanks bizoo, very handy :smile:

0 Likes