You are here: PSPad forum > English discussion forum > Sort lines by lenght of the string

Sort lines by lenght of the string

Goto Page: 1 2 Next

#1 Sort lines by lenght of the string

Posted by: Esgrimidor | Date: 02/18/2017 14:58 | IP: IP Logged

Each line in the txt file is a string
How to sort from the short to the longest ?

Best Regards

--
Nice program indeed

Options: Reply | Quote | Up ^


#2 Re: Sort lines by lenght of the string

Posted by: pspad | Date: 02/18/2017 21:58 | IP: IP Logged

Hello
Cause it's a first time I see this request, PSPad doesn't know it.
You can add this functionality by writing own script using PSPad scripting.
It's not too hard to add it into SORT dialog as option, but I need to know rules in case 2 lines has same length. What should be expected result?

Options: Reply | Quote | Up ^


#3 Re: Sort lines by lenght of the string

Posted by: Esgrimidor | Date: 02/18/2017 22:46 | IP: IP Logged

same length simply mantaine the initial order.
Or if you prefer in alphabetical order in the group of strings with the same length .
The important part for me is obtain grouped the strings with the same length

Best Regards

--
Nice program indeed

Options: Reply | Quote | Up ^


#4 Re: Sort lines by lenght of the string

Posted by: Stefan | Date: 02/20/2017 10:14 | IP: IP Logged

pspad:
You can add this functionality by writing own script using PSPad scripting.

.

// =========================================== Start 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));
}
// =========================================== End Code

.

slice() to remove additional added linebreak on each run.

.

HTH? grinning smiley

--
Stefan (pleased user since 1722)
Do you know you can spend Jan a beer? (click here)
Inofficial FAQs + Infos + Special Settings

Edited 1 time(s). Last edit at 02/20/2017 10:15 by Stefan.

Options: Reply | Quote | Up ^


#5 Re: Sort lines by lenght of the string

Posted by: Esgrimidor | Date: 02/20/2017 13:48 | IP: IP Logged

I will try and comment

Best Regards

--
Nice program indeed

Options: Reply | Quote | Up ^


#6 Re: Sort lines by lenght of the string

Posted by: Esgrimidor | Date: 02/20/2017 14:01 | IP: IP Logged

Ejem.

I don't know how to load the script.

Can you help me ?

Best Regards

--
Nice program indeed

Options: Reply | Quote | Up ^


#7 Re: Sort lines by lenght of the string

Posted by: Stefan | Date: 02/20/2017 14:39 | IP: IP Logged

Esgrimidor:
I don't know how to load the script.
Can you help me ?

Yes.

Store code as "...PSPad\Script\JScript\Sort.js"
Open PSPad.
Press F1 key.
Go to "Customize PSPad" > "Script User's Guide"
(also read "Script Menu" -Topic there)

Edited 1 time(s). Last edit at 02/20/2017 14:42 by Stefan.

Options: Reply | Quote | Up ^


#8 Re: Sort lines by lenght of the string

Posted by: pspad | Date: 02/20/2017 14:42 | IP: IP Logged

I guess the first step will be allow scripting in PSPad settings / System integration

Options: Reply | Quote | Up ^


#9 Re: Sort lines by lenght of the string

Posted by: Stefan | Date: 02/20/2017 15:06 | IP: IP Logged

If I may,.... some additional info for to update the help:


"Customize PSPad" > "Script User's Guide"
.
Script User's Guide
...
Where are the scripts stored
Scripts are organised in subdirectories coresponding to language. The root folder for scripts comes as subfolder .\Script.

....
Script files are plain text files stored in that subdirectories, with an extension, corresponding to the script language used,
e.g. "vbs" for VBScript (Visual Basic Script) or "js" for JScript (MS JScript and JavaScript).
If you have found an script on the forum, save the content inside of the code block as such a file in the right folder.
Be sure "System integration > Integrated scripting support (WSH)" is enabled in PSPad.
Next find the script in the "Script Menu", the Name (caption and sub menu, if any) is taken
from the script section "addMenuItem("caption","submenu","procedure to execute")"
(See "...\PSPad\Script\readme.rtf" for more information and some example scripts)
Note that you have to execute "Script Menu > Recompile scripts" each time the script code was modified.
Just click the wanted script entry from "Script Menu" to execute that script.

.

.

.

.

--
Stefan (pleased user since 1722)
Do you know you can spend Jan a beer? (click here)
Inofficial FAQs + Infos + Special Settings

Edited 2 time(s). Last edit at 02/20/2017 15:09 by Stefan.

Options: Reply | Quote | Up ^


#10 Re: Sort lines by lenght of the string

Posted by: Esgrimidor | Date: 02/21/2017 11:06 | IP: IP Logged

What is the meaning of :

".

slice() to remove additional added linebreak on each run.

."

--
Nice program indeed

Options: Reply | Quote | Up ^


Goto Page: 1 2 Next





Editor PSPad - freeware editor, © 2001 - 2017 Jan Fiala
Hosted by Webhosting TOJEONO.CZ, design by WebDesign PAY & SOFT, code Petr Dvořák