TryParse, LINQ. and numerical ordering in C#

smackababy

Lifer
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:
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,
 

LevelSea

Senior member
Jan 29, 2013
942
53
91
Is this what you're talking about? I didn't check they syntax btw, but I think something like this would work. This will get all the files with a numeric extension, then you can order it if you want.
Code:
        var f = Directory.GetFiles(path).Where(x =>
        {
            int y;
            return int.TryParse(Path.GetExtension(x).Remove(0,1), out y);
        });
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
I need the files without numeric extensions as well though. I just don't need them sorted.

So you want the files with the numeric extensions in sorted order, followed by the files without numeric extensions in any order? I'm not 100% clear on the desired behavior...

Anyway, if that's what you want, there are two approaches. One uses parsing, the other pads the file extension so that lexical ordering becomes consistent with numeric ordering.

This is conceptual; I have not tested it.

Option 1:
Code:
int surrogateComparisonKey = int.MaxValue;
var result = new DirectoryInfo("c:\foo").GetFiles()
    .OrderBy(x =>
    {
        int key;
        return int.TryParse(x.Extension, out key) ? key : surrogateComparisonKey;
    }).Select(x => x.FullName);

Option 2:
Code:
int maxNumericCharacters = int.MaxValue.ToString().Length;
var result = new DirectoryInfo("c:\foo").GetFiles()
    .OrderBy(x => x.Extension.PadLeft(maxNumericCharacters, '0'))
    .Select(x => x.FullName);
 
Last edited:

smackababy

Lifer
Oct 30, 2008
27,024
79
86
So you want the files with the numeric extensions in sorted order, followed by the files without numeric extensions in any order? I'm not 100% clear on the desired behavior...

Anyway, if that's what you want, there are two approaches. One uses parsing, the other pads the file extension so that lexical ordering becomes consistent with numeric ordering.

This is conceptual; I have not tested it.

Option 1:
Code:
int surrogateComparisonKey = int.MaxValue;
var result = new DirectoryInfo("c:\foo").GetFiles()
    .OrderBy(x =>
    {
        int key;
        return int.TryParse(x.Extension, out key) ? key : surrogateComparisonKey;
    }).Select(x => x.FullName);

Option 2:
Code:
int maxNumericCharacters = int.MaxValue.ToString().Length;
var result = new DirectoryInfo("c:\foo").GetFiles()
    .OrderBy(x => x.Extension.PadLeft(maxNumericCharacters, '0'))
    .Select(x => x.FullName);
Sorry for not responding sooner, the desired behavior would be define a default if the numeric conversion failed.
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
Sorry for not responding sooner, the desired behavior would be define a default if the numeric conversion failed.

Well, I tried my best. I still don't quite understand what you're saying. "Define a default" what, exactly?
 

smackababy

Lifer
Oct 30, 2008
27,024
79
86
Well, I tried my best. I still don't quite understand what you're saying. "Define a default" what, exactly?

Define something to default in terms of the int.Parse. I can't figure out if I can TryParse in line somehow. It's not a huge deal. Was just messing around and curious if it was possible. =)
 

slugg

Diamond Member
Feb 17, 2002
4,723
80
91
Define something to default in terms of the int.Parse. I can't figure out if I can TryParse in line somehow. It's not a huge deal. Was just messing around and curious if it was possible. =)

My first example does exactly that. Rename "surrogateComparisonKey" to "default" and voila!
 

smackababy

Lifer
Oct 30, 2008
27,024
79
86
My first example does exactly that. Rename "surrogateComparisonKey" to "default" and voila!

Sorry, didn't see you were using max value of integer as the default. Must have read it either early in the morning or late in the evening. =)

That should do exactly what I want. Thanks!