DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

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

Ath

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Sort lines of a txt file by lenght of the string
« Reply #25 on: February 20, 2017, 07: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:)
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

Contro

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

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 122
Re: Sort lines of a txt file by lenght of the string
« Reply #27 on: February 20, 2017, 05:59 PM »
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,649
    • Participated in NANY Challenge Eleven Times
    • Coding Snacks
    • Linux User
    • Hardware Hacker
Re: Sort lines of a txt file by lenght of the string
« Reply #28 on: February 20, 2017, 06: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.
/Ton
Ath's software collection, Quote: "A problem well stated is a problem half-solved." (Charles Kettering)

Lintalist

  • Participant
  • Joined in 2015
  • *
  • Posts: 122
Re: Sort lines of a txt file by lenght of the string
« Reply #29 on: February 20, 2017, 07: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

  • Member #520
  • Charter Honorary Member
  • Joined in 2005
  • ***
  • Posts: 1,150
Re: Sort lines of a txt file by lenght of the string
« Reply #30 on: February 21, 2017, 07: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
Code
//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:
Code
document.selection.text = ((document.selection.text.split("\n")).sort(function(a,b){return b.length-a.length})).join('\n');



 
Greetings, Stefan.

Contro

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

Original address: https://www.donationcoder.com/forum/index.php?topic=43525.0