DonationCoder.com forum
 
Read-only archive. This is a static copy of the DonationCoder.com forum. Reading and searching work as before; logging in, posting and private messages do not.
Archive of 2026-10-21 · About this archive · Main site

Author Topic: IDEA: Natural line sorting in file  (Read 5,997 times)

TucknDar

  • Advanced coder of Nowt
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 1,133
IDEA: Natural line sorting in file
« on: June 22, 2009, 08:23 PM »
Ok, I suppose any decent text editor should be able to sort lines based on alphabet or numbers, but I'm looking for a tool that can sort lines based on Natural order sorting, i.e.

from this:
Quote
1. some text
10. yet more text
1011. Text
2. more text again
4. more text
to:
Quote
1. some text
2. more text again
4. more text
10. yet more text
1011. Text

No GUI necessary, btw. command line tool would be perfect :)

thanks!

kfitting

  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 593
Re: IDEA: Natural line sorting in file
« Reply #1 on: June 22, 2009, 08:47 PM »
See the following websites for more general info:
http://www.codinghorror.com/blog/archives/001018.html
http://www.codeproject.com/KB/string/NaturalSortComparer.aspx

I've done an algorithm for excel using the Windows API function in XP... someone might be able to use that to simplify things.

Kevin

skwire

  • I eat Coding Snacks for breakfast...
  • Global Moderator
  • Joined in 2005
  • *****
  • Posts: 5,297
    • Participated in NANY Challenge Seventeen Times
    • Coding Snacks
    • Superboy Coder
    • Cody Award
    • Volunteers/Staff
    • Gathering Badge
Re: IDEA: Natural line sorting in file
« Reply #2 on: June 23, 2009, 05:25 PM »
No GUI necessary, btw. command line tool would be perfect :)

The Unix "sort -n" command will do this easily. 

Code

skwire@hankoku ~
$ clip
1. some text
10. yet more text
1011. Text
2. more text again
4. more text

skwire@hankoku ~
$ clip | sort -n
1. some text
2. more text again
4. more text
10. yet more text
1011. Text

skwire@hankoku ~

I've attached a Win32 binary version of the Unix sort command.

sort.zip (30.1 KB)

TucknDar

  • Advanced coder of Nowt
  • Charter Member
  • Joined in 2005
  • ***
  • Posts: 1,133
Re: IDEA: Natural line sorting in file
« Reply #3 on: June 23, 2009, 05:35 PM »
Thanks, skwier, works great :)

Original address: https://www.donationcoder.com/forum/index.php?topic=18890.0