- Oct 30, 2008
- 27,024
- 79
- 86
Guys,
A little help would be nice. I don't have a ton of C# experience and even less LINQ experience.
I am messing around with a project at home and am trying to order a list numerically, rather than alpha numerically. The issue isn't the actual ordering part, I have no problem figuring that out, but I want to be sure, without iterating each file, they contain a number as the extension.
The files are as such:
Now, I am not certain every file will end with a numerical value. So, I need to order these. This is what I have, but I need a TryParse in the actual order, as the list above will fail. Some kind of default on error would be ideal, but where that one ends up isn't so much a problem, just that it doesn't cause an error.
If there is a better way to do this, that would also help. I just need to know that it won't be ordered alphanumerically.
Thanks,
A little help would be nice. I don't have a ton of C# experience and even less LINQ experience.
I am messing around with a project at home and am trying to order a list numerically, rather than alpha numerically. The issue isn't the actual ordering part, I have no problem figuring that out, but I want to be sure, without iterating each file, they contain a number as the extension.
The files are as such:
Code:
file.1
file.2
file.3
...
file.10
file.11
file.lin
Now, I am not certain every file will end with a numerical value. So, I need to order these. This is what I have, but I need a TryParse in the actual order, as the list above will fail. Some kind of default on error would be ideal, but where that one ends up isn't so much a problem, just that it doesn't cause an error.
Code:
FileInfo[] files = dir.GetFiles(searchFile).Where(f => !f.Name.EndsWith("_TESTING")).ToArray<FileInfo>();
int z = -1; //required for TryParse; does nothing
if (split[split.Length - 1].LastIndexOf('.') > 0 && int.TryParse(rawFileString.Substring(rawFileString.LastIndexOf('.') + 1), out z))
{
files = files.OrderBy(s => int.Parse(s.Name.Substring(s.Name.LastIndexOf('.') + 1))).ToArray<FileInfo>();
}
If there is a better way to do this, that would also help. I just need to know that it won't be ordered alphanumerically.
Thanks,