topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Tuesday March 19, 2024, 1:46 am
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: Sort lines of a txt file by lenght of the string  (Read 20115 times)

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #25 on: February 20, 2017, 01:48 AM »
Did you re-try the TED Notepad procedure already? I'd be interested to hear if you got that working. Eventually that's the simplest procedure, while keeping your files local.

I think very interesting to load a python script.
It's new for me.

Can I try again ?
Unfortunately, Python is an indentation sensitive language, so normal lines need to start in column 1, and anything grouped in conditional or for loops needs to be indented. (It lacks the curly braces from C/Java style languages, or the begin/end from Pascal.) Copying from the forum inserts the undesired white-space :(
It should work when you remove the excess white-space from the python script, only 3 lines should be indented, all other lines need to start at column 1. (How to un-indent: Select all indented lines, press Shift-Tab once)
I'll update the procedure, (or mouser could fix the forum to not insert white-space when copying text from a code block... :tellme:)

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #26 on: February 20, 2017, 06:46 AM »
Running to try. I will comment !

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 120
    • View Profile
    • Lintalist
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #27 on: February 20, 2017, 11:59 AM »
Did you re-try the TED Notepad procedure already?

Perhaps very long lines and wrapped text cause it to fail?

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,610
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #28 on: February 20, 2017, 12:41 PM »
Did you re-try the TED Notepad procedure already?

Perhaps very long lines and wrapped text cause it to fail?
That's unlikely, TED Notepad supports very long lines (over 1 million characters), and isn't fooled by wrapped lines either.

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 120
    • View Profile
    • Lintalist
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #29 on: February 20, 2017, 01:45 PM »
That's unlikely, TED Notepad supports very long lines (over 1 million characters), and isn't fooled by wrapped lines either.
Good for TED :Thmbsup:

(was just a thought, some editors have issues with very long lines)

AbteriX

  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,149
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #30 on: February 21, 2017, 01:57 AM »
Sort lines of a txt file by lenght of the string

With pure JavaScript and a TextEditor supporting WSH scripting.

NewText = (OldText().split("\n")).sort(function(a,b){return b.length-a.length}).join('\n');



PSPad
//X:\PSPad\Script\JScript\Sort.js
module_name = "StefanSort";
module_ver = "0.001a";
function Init(){
  addMenuItem("Sort Random"                        ,"Sort", "StefanSortRandom");
  addMenuItem("Sort by line length (Long to short)","Sort", "StefanSortByLineLengthDown");
  addMenuItem("Sort by line length (Short to Long)","Sort", "StefanSortByLineLengthUp");
}
function StefanSortRandom() {
  var objEditor = newEditor();
  objEditor.assignActiveEditor();
  objEditor.Text((objEditor.Text().split("\n")).sort(function(){return .5 - Math.random()}).join("\n").slice(0, -1));
}
function StefanSortByLineLengthDown() {
  var objEditor = newEditor();
  objEditor.assignActiveEditor();
  objEditor.Text((objEditor.Text().split("\n")).sort(function(a,b){return b.length-a.length}).join('\n').slice(0, -1));
}
function StefanSortByLineLengthUp() {
  var objEditor = newEditor();
  objEditor.assignActiveEditor();
  objEditor.Text((objEditor.Text().split("\n")).sort(function(b,a){return b.length-a.length}).join('\n').slice(0, -1));
}



EmEditor:
document.selection.text = ((document.selection.text.split("\n")).sort(function(a,b){return b.length-a.length})).join('\n');



 

Contro

  • Supporting Member
  • Joined in 2007
  • **
  • Posts: 3,940
    • View Profile
    • Donate to Member
Re: Sort lines of a txt file by lenght of the string
« Reply #31 on: March 04, 2017, 05:18 AM »
Problem solved.
I have tried some of the above help with good result.
 :-[