-1

I have the following files in a List<string>

backup-codes (7).txt
backup-codes.txt
backup-codes (1).txt
backup-codes (2).txt
backup-codes (3).txt
backup-codes (8).txt
backup-codes (6).txt

How can I sort them by the number (eg. (#) ) so that files with the highest value always appear first in the list?

TheAuzzieJesus
  • 587
  • 9
  • 23
  • @TinyGiant: bad duplicate suggestion. Here OP is asking for sorting the string based on the numeric value inside the string – sujith karivelil Apr 11 '17 at 04:44
  • 2
    @un-lucky wouldn't that just be a combination of _How to sort a List_ and _How to extract a number from a string_? Do we need to have a _How do I sort my list by W character of type X at position Y in input of type Z?_ for every W, X, Y, and Z? –  Apr 11 '17 at 04:59
  • @un-lucky that is because most questions asked today _are_ duplicates, at least for the most mature technologies. –  Apr 11 '17 at 05:01

1 Answers1

2

You can do something like

lstFiles = lstFiles.OrderByDescending(x=> int.Parse(Regex.Replace(x,"[^0-9]+","0"))).ToList<string>();

This assumes that there are no other digits in filename except for sequencing.

Here is fiddler : https://dotnetfiddle.net/Nb38fJ

Hakunamatata
  • 1,275
  • 13
  • 18