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.
    • 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