You are here: PSPad forum > English discussion forum > Re: Sorting limited to certain columns - how?

Re: Sorting limited to certain columns - how?

Goto Page: 1 2 Next

#1 Sorting limited to certain columns - how?

Posted by: dirks | Date: 2015-11-24 15:46 | IP: IP Logged

For a comparison I need to sort blocks of XML lines. The original order is this:

<Meta name="ger_key_id" value="1" />
<Meta name="ger_key_id" value="3" />
<Meta name="ger_key_id" value="2" />
<Meta name="lgk_key_id" value="1" />
<Meta name="fbr_key_id" value="8" />
<Meta name="fbr_key_id" value="1" />
<Meta name="grt_key_id" value="1" />
<Meta name="rol_key_id" value="8" />
<Meta name="rol_key_id" value="4" />
<Meta name="rol_key_id" value="6" />
<Meta name="rol_key_id" value="2" />
<Meta name="rol_key_id" value="13" />
<Meta name="rol_key_id" value="14" />

What I wanted is that the blocks are sorted by the name of the xxx_key_id and that the order within the blocks is preserved, like so (sorted by hand):

<Meta name="fbr_key_id" value="8" />
<Meta name="fbr_key_id" value="1" />
<Meta name="ger_key_id" value="1" />
<Meta name="ger_key_id" value="3" />
<Meta name="ger_key_id" value="2" />
<Meta name="grt_key_id" value="1" />
<Meta name="lgk_key_id" value="1" />
<Meta name="rol_key_id" value="8" />
<Meta name="rol_key_id" value="4" />
<Meta name="rol_key_id" value="6" />
<Meta name="rol_key_id" value="2" />
<Meta name="rol_key_id" value="13" />
<Meta name="rol_key_id" value="14" />

What I get when I use "Specify Column" with Column Begin: 15 and Column End: 17
is this:

<Meta name="fbr_key_id" value="8" />
<Meta name="fbr_key_id" value="1" />
<Meta name="ger_key_id" value="2" />
<Meta name="ger_key_id" value="1" />
<Meta name="ger_key_id" value="3" />
<Meta name="grt_key_id" value="1" />
<Meta name="lgk_key_id" value="1" />
<Meta name="rol_key_id" value="2" />
<Meta name="rol_key_id" value="13" />
<Meta name="rol_key_id" value="14" />
<Meta name="rol_key_id" value="8" />
<Meta name="rol_key_id" value="4" />
<Meta name="rol_key_id" value="6" />

The lines got sorted by key-names (correctly), but the values within the blocks of equal key names are now in a different order (except for the fbr_key_ids), the order it seems rather random. Now what? Is there a way to get what I want?
Thanks in advance!

Edited 1 time(s). Last edit at 2015-11-24 15:52 by dirks.

Options: Reply | Quote | Up ^


#2 Re: Sorting limited to certain columns - how?

Posted by: pspad | Date: 2015-11-24 17:50 | IP: IP Logged

Sort it from clomn 15 to end of line, include value

Options: Reply | Quote | Up ^


#3 Re: Sorting limited to certain columns - how?

Posted by: dirks | Date: 2015-11-25 10:45 | IP: IP Logged

pspad:
Sort it from clomn 15 to end of line, include value

Then I get:

<Meta name="fbr_key_id" value="1" />
<Meta name="fbr_key_id" value="8" />
<Meta name="ger_key_id" value="1" />
<Meta name="ger_key_id" value="2" />
<Meta name="ger_key_id" value="3" />
<Meta name="grt_key_id" value="1" />
<Meta name="lgk_key_id" value="1" />
<Meta name="rol_key_id" value="13" />
<Meta name="rol_key_id" value="14" />
<Meta name="rol_key_id" value="2" />
<Meta name="rol_key_id" value="4" />
<Meta name="rol_key_id" value="6" />
<Meta name="rol_key_id" value="8" />

which is even more wrong, the whole lines are now sorted in lexicographical order, thereby changing
<Meta name="fbr_key_id" value="8" />
<Meta name="fbr_key_id" value="1" />
to
<Meta name="fbr_key_id" value="1" />
<Meta name="fbr_key_id" value="8" />
and
<Meta name="ger_key_id" value="1" />
<Meta name="ger_key_id" value="3" />
<Meta name="ger_key_id" value="2" />
to
<Meta name="ger_key_id" value="1" />
<Meta name="ger_key_id" value="2" />
<Meta name="ger_key_id" value="3" />
which must not happen. Not to speak of the re-ordering that happens with the "rol_key_id"-lines.

Hmm. If this is not a mistake and the sorting algorithm can't be told to keep the existing order as long as it does not matter for sorting, then I probably need an external script...

Options: Reply | Quote | Up ^


#4 Re: Sorting limited to certain columns - how?

Posted by: pspad | Date: 2015-11-25 11:23 | IP: IP Logged

Cause ordering uses Quicksort algorithm. You need to find tool which will order you first column as text and second column as number and the tool must be able to parse and sort XML

Or number your values with 01, 02 and it will be sorted correctly

Edited 1 time(s). Last edit at 2015-11-25 11:23 by pspad.

Options: Reply | Quote | Up ^


#5 Re: Sorting limited to certain columns - how?

Posted by: dirks | Date: 2015-11-25 13:42 | IP: IP Logged

Ah smiling smiley Thank you.

I will try with a (yet to write) special script.

Options: Reply | Quote | Up ^


#6 Re: Sorting limited to certain columns - how?

Posted by: dirks | Date: 2015-11-25 14:10 | IP: IP Logged

Just for clarification:

pspad:
Or number your values with 01, 02 and it will be sorted correctly

No, the important part of the problem ist that the order of lines that have the same key must stay the same, whatever the original order is, so if the lines contain the values
8
1
in this order (the fbr_key_id lines), that must NOT get sorted to
1
8
and if the values are originally
1
3
2
in this order (the ger_key_id lines), that must NOT get sorted to
1
2
3
- therefore, changing the values anyhow would not help at all.

I will find something suitable, Insertion Sort looks good - it "does not change the relative order of elements with equal keys", that's exactly what I need smiling smiley

Edited 1 time(s). Last edit at 2015-11-25 14:11 by dirks.

Options: Reply | Quote | Up ^


#7 Re: Sorting limited to certain columns - how?

Posted by: vbr | Date: 2015-11-25 18:38 | IP: IP Logged

dirks:
For a comparison I need to sort blocks of XML lines. The original order is this:

<Meta name="ger_key_id" value="1" />
<Meta name="ger_key_id" value="3" />
<Meta name="ger_key_id" value="2" />
<Meta name="lgk_key_id" value="1" />
<Meta name="fbr_key_id" value="8" />
<Meta name="fbr_key_id" value="1" />
<Meta name="grt_key_id" value="1" />
<Meta name="rol_key_id" value="8" />
<Meta name="rol_key_id" value="4" />
<Meta name="rol_key_id" value="6" />
<Meta name="rol_key_id" value="2" />
<Meta name="rol_key_id" value="13" />
<Meta name="rol_key_id" value="14" />

What I wanted is that the blocks are sorted by the name of the xxx_key_id and that the order within the blocks is preserved, like so (sorted by hand):

<Meta name="fbr_key_id" value="8" />
<Meta name="fbr_key_id" value="1" />
<Meta name="ger_key_id" value="1" />
<Meta name="ger_key_id" value="3" />
<Meta name="ger_key_id" value="2" />
<Meta name="grt_key_id" value="1" />
<Meta name="lgk_key_id" value="1" />
<Meta name="rol_key_id" value="8" />
<Meta name="rol_key_id" value="4" />
<Meta name="rol_key_id" value="6" />
<Meta name="rol_key_id" value="2" />
<Meta name="rol_key_id" value="13" />
<Meta name="rol_key_id" value="14" />

...

Hi,
you may try to add the temporary numbering as was suggested - you add the numbers to the original text just after _id" ) - using Alt+I (Edit: Lines manipulation: Insert text into lines)- Add numbering - e.g. at position 25.
You get
<Meta name="ger_key_id" 001 value="1" />
<Meta name="ger_key_id" 002 value="3" />
...

then you can sort on columns 13-27 and get:
<Meta name="fbr_key_id" 005 value="8" />
<Meta name="fbr_key_id" 006 value="1" />
<Meta name="ger_key_id" 001 value="1" />
<Meta name="ger_key_id" 002 value="3" />
<Meta name="ger_key_id" 003 value="2" />
<Meta name="grt_key_id" 007 value="1" />
<Meta name="lgk_key_id" 004 value="1" />
<Meta name="rol_key_id" 008 value="8" />
<Meta name="rol_key_id" 009 value="4" />
<Meta name="rol_key_id" 010 value="6" />
<Meta name="rol_key_id" 011 value="2" />
<Meta name="rol_key_id" 012 value="13" />
<Meta name="rol_key_id" 013 value="14" />

After that you just remove the intermediate numbers - using column select (Alt + mouse select)

hth,
vbr

Options: Reply | Quote | Up ^


#8 Re: Sorting limited to certain columns - how?

Posted by: dirks | Date: 2015-11-25 20:55 | IP: IP Logged

Hi vbr,

it works like charm, I just tried grinning smiley
And it solves my problem in the first place smiling smiley Yay! grinning smiley Thank you very much!

Right now I'm undecided whether using a script is worth the effort, but I'd need to do the series of operations everytime I load such a file into PSPad for comparison, which is several times a day (5-15 times, I guess)... so... every short-cut or automation of (a part of) these operations would be a very appreciated improvement - if you know of any such short-cut or automations, please tell! smiling smiley

(Meanwhile I have looked at sorting algorithms and found that there are several that provide a "stable" result and don't need to be slow... But so far I don't know yet how to call a script from PSPad and get the manipulated file or editor-object back.)

Edited 2 time(s). Last edit at 2015-11-25 20:57 by dirks.

Options: Reply | Quote | Up ^


#9 Re: Sorting limited to certain columns - how?

Posted by: vbr | Date: 2015-11-25 23:15 | IP: IP Logged

dirks:
Hi vbr,

it works like charm, I just tried grinning smiley
And it solves my problem in the first place smiling smiley Yay! grinning smiley Thank you very much!

Right now I'm undecided whether using a script is worth the effort, but I'd need to do the series of operations everytime I load such a file into PSPad for comparison, which is several times a day (5-15 times, I guess)... so... every short-cut or automation of (a part of) these operations would be a very appreciated improvement - if you know of any such short-cut or automations, please tell! smiling smiley

(Meanwhile I have looked at sorting algorithms and found that there are several that provide a "stable" result and don't need to be slow... But so far I don't know yet how to call a script from PSPad and get the manipulated file or editor-object back.)

Hi, you are welcome, I am glad, it helped.;
I just tried to write a javascript sorting function for this; you may try it on your input files and probably modify it to work as needed, also for possible cornercases.
The text is replaced for the original editor content, you may use copies, if needed.

just save the following e.g. as a js file in ...\PSPad\script\JScript\my_pspad_javascripts.js

Quote:
var module_name = "pspad_js";
var module_ver = "0.1";

function presortXML(){
if (editorsCount()<1){return;} // quit if no text window is open
var actEd = newEditor();
actEd.assignActiveEditor();
var inputTxt = actEd.text(); // get the whole text

var outputTxt = inputTxt; // perform some subsequent text conversions
outputTxt = outputTxt.replace(/(<Meta name="\S+_key_id)(.{0})(" value="\S+" \/>)/g, "$1____________$3"); // add placeholder for temporary numbering
var repl_nr_counter = 1000;
outputTxt = outputTxt.replace(/(____________)/g, function(m){repl_nr_counter+=1; return "__"+repl_nr_counter+"__";});// temporary sequential numbering - up to 900 items!
var linesArr = outputTxt.split("\r\n");
linesArr.sort(); // sort lines lexicographically respecting the temporary numbering
outputTxt = linesArr.join("\n");
outputTxt = outputTxt.replace(/__\d+__/g, ""); // remove the intermediate numbering
actEd.text(outputTxt); // set the modified text content
}

function Init(){
addMenuItem("presort", "", "presortXML", "Ctrl+Alt+Shift+W");// labels and shortcuts can be modified
}

After calling Script: Reload script (or on a fresh start of PSPad) this function should be available via script-menu or directly with the shortcuts set in the Init() function (this must not be taken by any other function etc.)

Please test it to match all your requirements -especially the content of the original data - the regex should match whole lines, you are interested in.
There must not be any data in the format used for intermediate numbering, i.e. __somenumber__ ; otherwise the sorting might break and the text could be corrupted.

Another limitaion of the current PSPad is the scripting support for unicode characters beyond the basic set Win-12... .
If such characters appear in the source, they would be removed with this script.

hth,
vbr

Options: Reply | Quote | Up ^


#10 Re: Sorting limited to certain columns - how?

Posted by: vbr | Date: 2015-11-26 08:10 | IP: IP Logged

Hi,
I'd like to add some clearer specification to the code posted above: forum.pspad.com

It changes the whole content of the editor window with the sorted text - it is probably ok for the given sample text which only contains the XML elements, that should be sorted;
however, it is likely, that a whole XML file will be loaded, with other elements, or at least with the toplevel element etc., where sorting would be harmful.
It might be appropriate to work with the selected text only.
The code above can be modified to work with the selected text only

Quote:
var inputTxt = actEd.text();

should be changed to:

Quote:
var inputTxt = actEd.selText();

and in the same way

Quote:
actEd.text(outputTxt);

to:

Quote:
actEd.selText(outputText);

Of course, there can be variants to check for a possibly present selection and handle it conditionally - depending on the requirements.
hth,
vbr

Options: Reply | Quote | Up ^


Goto Page: 1 2 Next





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