Sorting with a "column mode" selection



  • Hello, All,

    My God ! Although I’ve practiced the Notepad++ software since many years; do you know that I have not realized, yet, that we can sort text :

    • From a specific column till the end of line, with a zero-char column selection

    • From a n-chars key, between two columns, with a non-zero column selection

    I really do not understand how I could miss this important feature, for years :-(( Indeed, after verifications, this nice improvement exists since N++ version v6.7.9, which came out in June 2015 !

    It is these two lines, in the change.log file, of the last v7.9 version, which alerted me about a possible unknown feature ( for me ) !

    19. Fix column key sort with col block starting in col1 bug.
    24. Fix sorting by column key using incorrect key values.
    

    My ignorance of this feature explains why I proposed, in some posts, a work-around, based on regular expressions !!


    However, note that, in some cases, the use of a n-chars column selection or regex solutions do not give the same sort order ! For instance, let’s consider these lines, without their leading numbers, extracted from the change.log file of the last version :

    Add the ability to open the target file of a Shortcut (*.lnk).
    Add back hovered blue underlined URLs.
    Add an option to prevent right click from canceling selection.
    Add ability to sort lines randomly.
    Add word wrap option to find results context menu.
    Add keyboard navigation ability to Column Editor.
    Add keyboard shortcuts for switching to panels.
    Add confirmation preference for Replace All in All Opened Documents operation.
    Add Escape keypress in functionlist searchbox to clear its text.
    Add RTL capability to localized messageboxes.
    Add search scope to Find window status bar output.
    
    • If we create a 11 × 3 column selection, from column 1 and, then, use the option Edit > Line Operations > Sort lines Lexicographically Ascending, nothing has changed ! Logical : as the key Add is identical for all these lines, the initial order of the lines is simply kept !

    • Now, let’s imagine that I create a pseudo-key, from text between column 1 and 3, that I place before any text of each line, for further sorting, based on that pseudo-key. We can use the simple regex S/R :

      • SEARCH (?-s)^(...).+

      • REPLACE \1\t$0

    giving this list :

    Add	Add the ability to open the target file of a Shortcut (*.lnk).
    Add	Add back hovered blue underlined URLs.
    Add	Add an option to prevent right click from canceling selection.
    Add	Add ability to sort lines randomly.
    Add	Add word wrap option to find results context menu.
    Add	Add keyboard navigation ability to Column Editor.
    Add	Add keyboard shortcuts for switching to panels.
    Add	Add confirmation preference for Replace All in All Opened Documents operation.
    Add	Add Escape keypress in functionlist searchbox to clear its text.
    Add	Add RTL capability to localized messageboxes.
    Add	Add search scope to Find window status bar output.
    

    If we do a normal ( stream ) selection of these 11 lines and run the same sort, we get, this time :

    Add	Add Escape keypress in functionlist searchbox to clear its text.
    Add	Add RTL capability to localized messageboxes.
    Add	Add ability to sort lines randomly.
    Add	Add an option to prevent right click from canceling selection.
    Add	Add back hovered blue underlined URLs.
    Add	Add confirmation preference for Replace All in All Opened Documents operation.
    Add	Add keyboard navigation ability to Column Editor.
    Add	Add keyboard shortcuts for switching to panels.
    Add	Add search scope to Find window status bar output.
    Add	Add the ability to open the target file of a Shortcut (*.lnk).
    Add	Add word wrap option to find results context menu.
    
    • And, after deleting this leading pseudo-key with the S/R :

      • SEARCH (?-s)^.+\t

      • REPLACE Leave Empty

    We get our final sorted text :

    Add Escape keypress in functionlist searchbox to clear its text.
    Add RTL capability to localized messageboxes.
    Add ability to sort lines randomly.
    Add an option to prevent right click from canceling selection.
    Add back hovered blue underlined URLs.
    Add confirmation preference for Replace All in All Opened Documents operation.
    Add keyboard navigation ability to Column Editor.
    Add keyboard shortcuts for switching to panels.
    Add search scope to Find window status bar output.
    Add the ability to open the target file of a Shortcut (*.lnk).
    Add word wrap option to find results context menu.
    

    It’s obvious that, in this second solution, anything, after the leading Add word, is also sorted, giving a different order !

    If we extend this reasoning to a sorting with several keys ( solution possible with regexes but impossible with column selection as we cannot define multiple column selections ), this means that, in case that some lines have same sorting key(s), these lines are, then, normally sorted on their entire contents

    But, the behaviour of N++ sort, with a column selection, is also interesting as it keeps lines in their original order , in case of an equal sorting key !

    Best regards,

    guy038



  • @guy038 said in Sorting with a "column mode" selection:

    do you know that I have not realized, yet, that we can sort text…

    Welcome to the party. :-)

    I thought I would find info about this in the user manual, but I didn’t see anything about sorting in there; probably I am just not good at navigating the user manual. :-(

    …as the key Add is identical for all these lines, the initial order of the lines is simply kept !

    the behaviour of N++ sort, with a column selection, is also interesting as it keeps lines in their original order , in case of an equal sorting key !

    This is what is known as a “stable” sort. Some sort algorithms will mix up the original order when the key field is the same. Not, apparently, the N++ sort algo.

    I create a pseudo-key, from text between column 1 and 3, that I place before any text of each line, for further sorting,

    Seems pointless, as Add already leads off every line, but I think I get your point: If you wanted some variable data from each line, from a point embedded deeper into the liens…



  • @Alan-Kilborn said in Sorting with a "column mode" selection:

    I didn’t see anything about sorting in there

    That is being rectified to a certain extent – PR#118 was just accepted and merged a few days ago, so will be in the next release of the npp-user-manual.org website.

    However, since I also didn’t know that sorting works in column mode until this topic, that wasn’t mentioned there. What do you guys think if I added a bullet point to the Line Operations sorting-algorithm list:

    • If a Column Mode selection is active, the sort will re-order all the lines included in the selection, but the sort key (the text that decides the sort order) will be limited to what is inside the column selection. If the keys are identical on two lines, then the order of those two lines will not change (even if text outside of the selected key columns is different).


  • @PeterJones @guy038

    What do you guys think if I added a bullet point to the Line Operations sorting-algorithm list?

    👍

    Okay, so now I’m wondering how it is that I knew about it before this?

    It seems like I’ve always known about it, but I wasn’t born knowing it…

    I think it might have come from the bowels of this thread and the scripting that was derived from it: https://community.notepad-plus-plus.org/topic/14129/sort-file-removing-duplicates-possible/

    but again, not sure…



  • Hello,@guy038
    Follow this steps,To Sorting with a “column mode” selection

    Step 1:- Line Up Columns using the TextFX Plugin

    Step 1 :- Select the pipe (|) character that separates the “columns” and copy it to the clipboard:
    Step 2 :- Select all the text and use Line up multiple lines by (Clipboard Character):
    Step 3 :- We have now lined up the text using the pipe (|) character to make it look like a table with columns

    Step 2:- Move and Sort Columns

    Step 4:- Use Alt+Mouse Dragging or Alt+Shift+Arrows to select the “colums” you want to move.

    Step 5:- Drag the columns to the start of the first line. You can also Cut (CTRL+X) the selected columns, move the caret to the start of the first line, then Paste (CTRL+V) the columns there.

    Step 6:- Select all the text and use Sort Lines Lexicographically Ascending:

    Step 7:- We have now sorted the text alphabetically by the first “columns”:

    I hope this information will be usefull for you.
    Thank you.


Log in to reply