Menu

Python, sort lines by strings length

Help
fb16
2012-10-19
2013-01-25
  • fb16

    fb16 - 2012-10-19

    Hello,
    I don't know python. I've put together bits of code from the web to sort lines by the length of strings, i.e. to sort these lines:

    stri
    stri
    string
    string1
    string1
    s
    st
    str
    

    into these (removing duplicates also):

    string1
    string
    stri
    str
    st
    s
    

    Is there a way to streamline the following code i'm using, or maybe a simpler way to achieve the same result? Thanks

    editor.beginUndoAction()
    lines = {}
    if (editor.getSelText()):
        hm = editor.getSelText().splitlines()
        howmanyLines = len(hm)
    else:
        hm = editor.getLineCount()
        howmanyLines = hm
    for line in range(0, howmanyLines):
        strippedLine = editor.getLine(line).rstrip('\r\n ')
        if len(strippedLine) in lines:
            continue
        else:
            lines[len(strippedLine)]=[strippedLine]
    editor.clearAll()
    for l in reversed(sorted(lines)):
        console.write( "\r\n".join(reversed(sorted(lines[l]))) )
        print "\r\n".join(reversed(sorted(lines[l])))
    editor.endUndoAction()
    
     
  • fb16

    fb16 - 2012-10-23

    Hello,
    I've posted a new script here. For this reason I'm going to close this thread. Please, refer to this link in future:
    https://sourceforge.net/projects/notepad-plus/forums/forum/1290590/topic/6056194
    Best regards
    fb16

     

Log in to post a comment.