Sort as "aaAAbbBBccCC" or "AAaaBBbbCCcc" but not "AABBCCaabbcc", "aabbccAABBCC", or "aAAaBbbBCcCc"
-
@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…
-
“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.
-
@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… -
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
-
@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! :-)
-
It took a bit to find it for some reason, but here’s basically the same script as Peter’s more recent one:
-
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.
-
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 theA
line, the sort key will beaa
vsaA
, so it will put the upper-case first:editor.setText("\r\n".join(sorted(editor.getText().splitlines(), key=lambda s: s.lower()+s)))
=>
AAaaBBbbCCcc
-
Sorry, I also didn’t notice what was really wanted here. :-(
-
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.
-
@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. :-) -
@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. :-)
-
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.”. :)
-
@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. :-)