Sort Lines Lexicographically did not work



  • Just had the same problem. In MY case, the reason was: The file was protected from changes (was opened from TMP folder). I could not type anything.

    Once I had saved it as a new file on the desktop, I could type, and thus sorting also worked.



  • It seems that in Notepadd++ 7.7.1 there needs to be at least one empty line after the last line of text, otherwise the sorting function doesn’t do anything.

    If end of file is in an empty line after the last line of text, sorting works:
    text
    text
    <EOF>

    If end of file is at the end of the last line of text, sorting doesn’t work:
    text
    text<EOF>



  • @r-l said:

    in Notepadd++ 7.7.1 there needs to be at least one empty line after the last line of text, otherwise the sorting function doesn’t do anything

    This is NOT true when I try it. Can you provide anything more to help reproduce?



  • You’re right. And I just realized what my mistake was.

    I had searched and replaced certain character sequences prior to sorting, to insert linebreaks. In Notepad++ that linebreak must be “\r\n” rather than just “\n”. With correct linebreaks, sorting works fine. With just “\n”, the sorting function sees the whole file as one single line. Likewise, when I add an empty line at the end of the file, it only sorts that empty line to the beginning of the file but doesn’t change the sequency of anything else.

    So, sorry for the noise… :-)



  • @r-l said:

    In Notepad++ that linebreak must be “\r\n” rather than just “\n”

    Only if you want it to be. There are Windows files (\r\n) and there are Linux (\n) files when it comes to “line endings”. If you are doing regular expression replacements, you absolutely need to be careful about using \r\n versus using \n. There is nothing there to protect you from yourself.

    And…sorting is known to have problems when you have mixed line-endings in the data, something you shouldn’t allow to happen unless you really know what you are doing, and really need that situation.



  • I often have problems sorting text in NotePaid++ - I’m set as Windows CR/LF etc. The problem seems to arise if spaces and tabs are interspersed as beginning of line characters…

    Here is an example:

    BuildAccount()            // Summary of Posted Transactions
    BuildAudAcct()            // Tax Records that relate to Account Records....
    BuildAudit()              // Daily & Account
    BuildBankRec()
    BuildCheque()
    BuildContainer()
    BuildCounterIDs()  // Apr 2020
    BuildDepot( )
    BuildEvents( )    // Dec 2019
    BuildFinBat()
    BuildGrower()
    BuildImpBat()
    BuildPayGrp()
    BuildPostBat()
    BuildPrice()
    BuildProcess()
    BuildProduct()
    BuildReceipts()
    BuildTax( )
    BuildVariety()    // June 2015
    

    BuildChqFmt() // Note the Spaces in front of these lines rather than tabs
    BuildRptCfg( ) // this seems to be the cause of the problems
    BuildVoid()



  • @Bill-Hepler

    The data in the black code region seems already sorted?

    What’s the significance of the 3 items below the code box?

    And I don’t see any leading spaces/tabs anywhere?



  • @Alan-Kilborn I suspect what happened there is that the lines DID have tabs and spaces, respectively, when they pasted it into their post, but the tabs caused the black box. The three after used a different whitespace, and so were not included in the black box. (But apparently the whitespace was formatted away?) Note that, if you expect sorting to ignore leading whitespace, the three lines after the box should be interspersed between lines within the box (but are not).



  • @Matthew-Ewer

    That’s a good explanation of what might have happened here.
    But since the OP never returned to continue the exchange, I suppose the issue is dead.

    if you expect sorting to ignore leading whitespace

    Just a note to say that Notepad++'s sorting doesn’t ignore leading whitespace, just in case someone thinks it can from the above.



  • This post is deleted!


  • Maybe it could be helpful for anyone else. I had this issue, and the problem was related with the line break codes.

    Even if you are seeing different lines, is possible that only one of the line break codes (\n or \r) is being used. If that’s the case, sorting won’t work as it will take all the text as just one single line. You can check this enabling the “Show all characters” option:

    2020-10-01 23_00_30-Window.png

    LF is the code for Line Feed (\n). But it’s missing the CR (Carriage Return, \r) code. Unlike Unix systems, standard line-termination in Windows and in the Internet is \r\n.

    So if you replace these line-termination codes activating the extended search mode (replacing \n with \r\n), it will work.

    2020-10-01 22_58_49-Window.png



  • @aarocaes ,

    Interesting. It sorts correctly for me.
    5cdccf20-469c-44e6-a765-074bf01d15ac-image.png

    I thought maybe if you have mixed line endings, where some don’t match the current line-endings mode:
    94dd079f-50dd-4ff3-ae3a-a124003337e8-image.png => 9cc139ab-cde8-41c8-89d1-10cb24fb14fa-image.png
    Yeah, that’s it: correctly\nsorted and these\nlines were treated as single lines.

    So, as long as you don’t have mixed line endings, and your line endings match the defined line endings setting shown in the status bar, it will work. But if there’s a mix or discrepancy, it won’t be treated as a line ending.


Log in to reply