• Login
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.
  • A
    Alan Kilborn @VTGroupGitHub
    last edited by Sep 10, 2020, 2:45 PM

    @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…

    A 1 Reply Last reply Sep 10, 2020, 2:52 PM Reply Quote 1
    • P
      PeterJones @VTGroupGitHub
      last edited by Sep 10, 2020, 2:50 PM

      @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
      • A
        Alan Kilborn @Alan Kilborn
        last edited by Sep 10, 2020, 2:52 PM

        @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! :-)

        A P 2 Replies Last reply Sep 10, 2020, 2:58 PM Reply Quote 0
        • A
          Alan Kilborn @Alan Kilborn
          last edited by Sep 10, 2020, 2:58 PM

          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
          • V
            VTGroupGitHub
            last edited by Sep 10, 2020, 2:59 PM

            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.

            P A 2 Replies Last reply Sep 10, 2020, 3:05 PM Reply Quote 0
            • P
              PeterJones @VTGroupGitHub
              last edited by Sep 10, 2020, 3:05 PM

              @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
              • A
                Alan Kilborn @VTGroupGitHub
                last edited by Sep 10, 2020, 3:06 PM

                @VTGroupGitHub

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

                P 1 Reply Last reply Sep 10, 2020, 3:08 PM Reply Quote 0
                • V
                  VTGroupGitHub
                  last edited by Sep 10, 2020, 3:08 PM

                  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
                  • P
                    PeterJones @Alan Kilborn
                    last edited by Sep 10, 2020, 3:08 PM

                    @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
                    • P
                      PeterJones @Alan Kilborn
                      last edited by Sep 10, 2020, 3:11 PM

                      @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
                      • V
                        VTGroupGitHub
                        last edited by Sep 10, 2020, 3:11 PM

                        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.”. :)

                        P 1 Reply Last reply Sep 10, 2020, 3:13 PM Reply Quote 1
                        • P
                          PeterJones @VTGroupGitHub
                          last edited by Sep 10, 2020, 3:13 PM

                          @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
                          13 out of 15
                          • First post
                            13/15
                            Last post
                          The Community of users of the Notepad++ text editor.
                          Powered by NodeBB | Contributors