Community
    • Login

    Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc"

    Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
    15 Posts 3 Posters 1.0k Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • VTGroupGitHubV
      VTGroupGitHub
      last edited by

      NPP 7.8.9 32-bit, Windows 10

      Given 12 lines that start with each one of these letters “aCcBAbbAcaCB”, “Edit / Line Operations / Sort Lines Lexicographically Ascending” will produce a sort that looks like “AABBCCaabbcc”. But what I want is a strictly alphabetical sort i.e. “aaAAbbBBccCC” or “AAaaBBbbCCcc”.

      I probably should have asked here first if there was an easy way, but instead I put together a Python script which assumes I’ve copied the lines to the clipboard, sends the data to the Windows command line to run its SORT command, then returns the results to the clipboard, so I can paste over the current text.

      I’m not a Python programmer, so this code is probably not optimal, but did I miss an easier way besides a Python script to do this?

      import os
      import subprocess
      import win32clipboard as cb
      import fileinput
      
      fp1 = "C:\\nppsort1.tmp"
      fp2 = "C:\\nppsort2.tmp"
      try:
          cb.OpenClipboard()
          data=cb.GetClipboardData()
      
          tempFile = open(fp1, "w")
          tempFile.write(data)
          tempFile.close()
      
          command = "sort " + fp1 + " /OUTPUT " + fp2
          process = subprocess.Popen(command.split(), stdout=subprocess.PIPE)
          output, error = process.communicate()
      
          with open(fp2, 'r') as f:
              results = f.read()
          cb.EmptyClipboard()
          cb.SetClipboardData(cb.CF_TEXT, results)
      
      finally:
          cb.CloseClipboard()
          os.remove(fp1)
          os.remove(fp2)
      
      Alan KilbornA 1 Reply Last reply Reply Quote 1
      • Alan KilbornA
        Alan Kilborn @VTGroupGitHub
        last edited by

        @VTGroupGitHub said in Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc":

        but did I miss an easier way besides a Python script to do this?

        Well, maybe.

        First, Notepad++ does not natively support a case-insensitive sort, which I think is what you are seeking?

        There is talk about it; see HERE, but an insensitive sort is not a straightforward thing. You probably think that it is, but if you bring Unicode and locales into the conversation, you rapidly see that it is not so clear cut.

        Second, your Python approach is a bit, well, roundabout.
        It’s great that it works, but…there are simpler and better ways.

        Notepad++ has a great plugin called Pythonscript, which gives you nice Python control of the editor and its data, right inside Notepad++. I’ll see what I can find for this, and I’ll post again…

        1 Reply Last reply Reply Quote 1
        • VTGroupGitHubV
          VTGroupGitHub
          last edited by

          “Roundabout” is a good word. :) That’s actually why I asked, because even though it works, I figured there had to be an easier way. And this script is actually running via the PythonScript plugin, and I’ve got a button on my toolbar now to run it.

          Any other ideas you have would be much appreciated.

          Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
          • Alan KilbornA
            Alan Kilborn @VTGroupGitHub
            last edited by

            @VTGroupGitHub said in Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc":

            And this script is actually running via the PythonScript plugin

            Ohhh… That’s even worse then. :-)
            I thought you were doing some sort of Run menu entry, shelling out to an independent Python.

            I’m still digging through previous posts on this topic.
            I could throw together yet another script to do it quickly, but it is probably better to just cite something that has previously been done.
            Check back…

            Alan KilbornA 1 Reply Last reply Reply Quote 1
            • PeterJonesP
              PeterJones @VTGroupGitHub
              last edited by

              @VTGroupGitHub ,

              from Npp import *
              editor.setText("\r\n".join(sorted(editor.getText().splitlines(), key=lambda s: s.lower())))
              

              Okay, so that’s the compressed way. To make it more intelligible:

              from Npp import editor                                  # make sure you know the `editor` object
              
              editor.beginUndoAction()                                # use the begin/end undo action to make a single ^Z undo the whole sorting action
              
              lines = editor.getText().splitlines()                   # get a list of the lines in the active editor window
              
              sorted_lines = sorted(lines, key=lambda s: s.lower())   # use the lowercase version of the text as the key for sorting, but the data itself doesn't change case
              
              joined = "\r\n".join(sorted_lines)                      # join the lines together
              
              editor.setText(joined)                                  # change the text in the  active editor window
              
              editor.endUndoAction()                                  # use the begin/end undo action to make a single ^Z undo the whole sorting action
              
              1 Reply Last reply Reply Quote 1
              • Alan KilbornA
                Alan Kilborn @Alan Kilborn
                last edited by

                @Alan-Kilborn said in Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc":

                I could throw together yet another script to do it quickly, but it is probably better to just cite something that has previously been done.

                Well, Peter violated this principle, but I’ve no problem with that! :-)

                Alan KilbornA PeterJonesP 2 Replies Last reply Reply Quote 0
                • Alan KilbornA
                  Alan Kilborn @Alan Kilborn
                  last edited by

                  It took a bit to find it for some reason, but here’s basically the same script as Peter’s more recent one:

                  https://community.notepad-plus-plus.org/post/55681

                  1 Reply Last reply Reply Quote 1
                  • VTGroupGitHubV
                    VTGroupGitHub
                    last edited by

                    Thanks, but unless I’m not seeing something different between your code and mine, I think this is what I tried first. Won’t it give you the “aAAaBbbBCcCc” results? That is, it sorts the aA’s in the order they’re in in the file, and not alphabetical AND case order.

                    PeterJonesP Alan KilbornA 2 Replies Last reply Reply Quote 0
                    • PeterJonesP
                      PeterJones @VTGroupGitHub
                      last edited by

                      @VTGroupGitHub ,

                      Sorry, didn’t notice that you wanted a combo of insensitive + sensitive.

                      Change the lambda expression. I think doing s.lower()+s will give you want you want. when it is comparing the a line to the A line, the sort key will be aa vs aA, so it will put the upper-case first:

                      editor.setText("\r\n".join(sorted(editor.getText().splitlines(), key=lambda s: s.lower()+s)))
                      

                      => AAaaBBbbCCcc

                      1 Reply Last reply Reply Quote 1
                      • Alan KilbornA
                        Alan Kilborn @VTGroupGitHub
                        last edited by

                        @VTGroupGitHub

                        Sorry, I also didn’t notice what was really wanted here. :-(

                        PeterJonesP 1 Reply Last reply Reply Quote 0
                        • VTGroupGitHubV
                          VTGroupGitHub
                          last edited by

                          Your solution sounds like it should work! I hadn’t considered “+s”, so will test as soon as I can and close this issue.

                          Thanks to both of you.

                          1 Reply Last reply Reply Quote 2
                          • PeterJonesP
                            PeterJones @Alan Kilborn
                            last edited by

                            @Alan-Kilborn said in Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc":

                            Sorry, I also didn’t notice what was really wanted here.

                            Since the only place he mentioned that he didn’t want that particular aAAaBbbBCcCc answer was in the “not” list in the title, I think we should be forgiven for not noticing. :-)

                            1 Reply Last reply Reply Quote 1
                            • PeterJonesP
                              PeterJones @Alan Kilborn
                              last edited by

                              @Alan-Kilborn said in Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc":

                              @Alan-Kilborn said in Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc":

                              I could throw together yet another script to do it quickly, but it is probably better to just cite something that has previously been done.

                              Well, Peter violated this principle, but I’ve no problem with that! :-)

                              Note that my googling for how to sort case-insensitive in python, coming up with a one liner myself, and then cleaning it up into a readable script took less time than trying to find a bit of code in this forum. I really like and appreciate this forum, but searching it is … not always helpful or efficient. :-)

                              1 Reply Last reply Reply Quote 1
                              • VTGroupGitHubV
                                VTGroupGitHub
                                last edited by

                                Agreed. And it’s funny you say that, as I almost made my title “Sort “aCcBAbbAcaCB” as “aaAAbbBBccCC” or “AAaaBBbbCCcc” but not “AABBCCaabbcc”, “aabbccAABBCC”, or “aAAaBbbBCcCc””, and my post body “See title.”. :)

                                PeterJonesP 1 Reply Last reply Reply Quote 1
                                • PeterJonesP
                                  PeterJones @VTGroupGitHub
                                  last edited by

                                  @VTGroupGitHub said in Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc":

                                  as I almost made … my post body “See title.”

                                  Thank you for not. That might’ve earned you a downvote on your OP from some users here, rather than getting you an answer. :-)

                                  1 Reply Last reply Reply Quote 1
                                  • First post
                                    Last post
                                  The Community of users of the Notepad++ text editor.
                                  Powered by NodeBB | Contributors