Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alphanumeric sorting order of strings in C [duplicate]

Possible Duplicate:
Natural sort in C - “array of strings, containing numbers and letters”

When sorting strings in C with qsort and strcmp I have the problem that alphanumeric entries, typically strings ending with numbers, are being sorted oddly like this:

  • Entry1
  • Entry12
  • Entry2

The desired behavior is this:

  • Entry1
  • Entry1_new
  • Entry2
  • Entry12

What is the easiest way to do this?

Thanks

like image 373
Chris Avatar asked Nov 21 '25 09:11

Chris


1 Answers

There's nothing odd about the sort; '1' comes before '2', so any string that has 'Entry1' will come before any string that has 'Entry2'. That's just the way strcmp is defined. If you desire a different sort order, you can always write a different sort function.

like image 190
Paul Sonier Avatar answered Nov 22 '25 23:11

Paul Sonier