How do I sort lines in numeric order in Notepad++?

Using Notepad++ v6.6.8 with TextFX.

How do I sort lines in numeric order rather than alphanumeric?

That is I want lines to sort like:

1
2
10
11
15
20

not:

1
10
11
15
2
20

Solution 1:

This is now easy to achieve (at least in Notepad++ 7.5.9):

Use the menu item: Edit -> Line Operations -> Sort Lines As Integers Ascending

(Note if you don't select any text it will sort the entire file, and if you select text it constrains the sorting to the selected text.)

Solution 2:

I don't know what your file looks like, but I'd use regular expressions to add spaces or zeros before each number to make them the same length (e.g. 2 becomes 002). Then they will sort correctly and you can use another replacement to strip the leading spaces/zeros afterwards.

These are the steps (works for number up to 10 characters)

Find: ^ Replace: 0000000000

Find: \d*(\d{10}) Replace: \1

Sort

Find: ^0* Replace:

It works by adding 10 zeros before the number, even though that's probably too much. The second replacement than takes the last 10 digits of the number to bring everything back to the same length, giving you numbers like 0000000839, 0000000003 etc. Those will sort in the order you want them to sort. Once sorted the last expression will strip all leading zeros so you'll have your original numbers back.

If you need longer numbers just add more zeros to the first replacement, and increase the 10 in the second replacement accordingly. If you're going to do this more often you could record a macro with these steps.

Solution 3:

I haven't tried this but there is a plugin which claims to do this (as long as the lines BEGIN with a number). Here is the link: http://www.scout-soft.com/linesort/

Update Ok, that plugin is apparently gone for now. Maybe it doesn't work with newer NP++ versions. Here is another one which I've seen in the plugin manager so it is at least more common: http://william.famille-blum.org/blog/index.php?entry=entry110123-113226

I just tried it on 6.6.9 and it is a little awkward (don't forget to hit the Add button on the dialog) but works perfectly well.