0

I'm getting all the files in a folder and putting them into a string array. The problem I'm having is the string array will not sort right with the pages that have a number larger than 9. What would be the easiest way to get the string array to sort the right way? As you can see the very first pdf is A10C and it should show up under A9C, but it's showing up first. Same thing is happening with B10C:

\\server\archives\PAPER\20140818\NJ-140818-A10C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A1C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A2C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A3C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A4C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A5C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A6C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A7C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A8C.pdf
\\server\archives\PAPER\20140818\NJ-140818-A9C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B10C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B1C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B2C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B3C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B4B.pdf
\\server\archives\PAPER\20140818\NJ-140818-B5C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B6C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B7C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B8C.pdf
\\server\archives\PAPER\20140818\NJ-140818-B9C.pdf
Chris Mantle
  • 6,595
  • 3
  • 34
  • 48
user1800738
  • 167
  • 2
  • 9
  • 1
    Please show your code. – tnw Aug 26 '14 at 19:27
  • 2
    What you want is a [Natural Sort](http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/), where numbers are treated as numbers instead of characters. – crashmstr Aug 26 '14 at 19:27
  • Natural sort: http://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp – David Crowell Aug 26 '14 at 19:27
  • why not just put them into a List and use the internal sorting that comes along with a List show more effort there are tons of examples on line [Array.Sort](http://www.csharp-examples.net/sort-array/) – MethodMan Aug 26 '14 at 19:30
  • Answers to [Natural sort](http://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp) cover pretty much all ways to extract number to compare to - make sure to check all answers if you think your problem is completely different. Feel free to update your post to show how your one is different and comment/flag to reopen. – Alexei Levenkov Aug 26 '14 at 19:35
  • 2
    @DJKRAZE The built-in sort will not give the results that the OP wants. – Code-Apprentice Aug 26 '14 at 19:38
  • 1
    user1800738, You can also try [this](http://stackoverflow.com/a/11052176/932418) – EZI Aug 26 '14 at 19:39

1 Answers1

0

you can use a combination of regex and linq

var sorted = strs
    .OrderBy(s => DateTime.ParseExact(Regex.Match(s, @"PAPER\\(\d+)\\").Groups[1].Value, "yyyyMMdd", CultureInfo.InvariantCulture))
    .ThenBy(s => Regex.Match(s, @"(\w)\d+\w.pdf").Groups[1].Value)
    .ThenBy(s => int.Parse(Regex.Match(s, @"\w(\d+)\w.pdf").Groups[1].Value))
    .ThenBy(s => Regex.Match(s, @"\w\d+(\w).pdf").Groups[1].Value);
  • Thanks guys natural sort was exactly what I was looking for, this code works perfectly. http://www.dotnetperls.com/alphanumeric-sorting – user1800738 Aug 26 '14 at 19:41