views:

586

answers:

1

SOLVED at the bottom of my post.

Or more specifically:

I have a bunch of FileInfo objects (I need the FileInfo objects to exclude hidden, system and reparse point files).

I need to sort FileInfo[] naturally based on their FileInfo.FullName. So FILE_10.ext should come after FILE_2.ext. Luckily the FileInfo[] contains files of only one extension.

I have implemented a comparer:

/// <summary>
/// Compares FileInfo objects based on the files full path.
/// This comparer is flawed in that it will only work correctly
/// on files with the same extension.
/// Though that could easily be fixed.
/// </summary>
private class FileInfoSorter : IComparer
{

    int IComparer.Compare(Object x, Object y)
    {
        FileInfo _x = x as FileInfo;
        FileInfo _y = y as FileInfo;

        // FYI: 
        //ExprFileVersion = new Regex("(.*)_([0-9]+)\\.[^\\.]+$", RegexOptions.Compiled);
        Match m1 = RegExps.ExprFileVersion.Match(_x.FullName);
        Match m2 = RegExps.ExprFileVersion.Match(_y.FullName);
        if (m1.Success && m2.Success) // we have versioned files
        {
            int n1;
            int n2;
            try
            {
                n1 = int.Parse(m1.Groups[2].Value);
            }
            catch (OverflowException ex)
            {
                // Don't know if this works.
                ex.Data["File"] = _x.FullName;
                throw;
            }

            try
            {
                n2 = int.Parse(m2.Groups[2].Value);
            }
            catch (OverflowException ex)
            {
                // Don't know if this works.
                ex.Data["File"] = _y.FullName;
                throw;
            }


            string s1 = m1.Groups[1].Value;
            string s2 = m2.Groups[1].Value;

            if (s1.Equals(s2))
            {
                return n1.CompareTo(n2); // compare numbers naturally. E.g. 11 > 6                        
            }
            else // not the same base file name. So the version does not matter.
            {
                return ((new CaseInsensitiveComparer()).Compare(_x.FullName, _y.FullName));
            }
        }
        else // not versioned
        {
            return ((new CaseInsensitiveComparer()).Compare(_x.FullName, _y.FullName));
        }
    }


}

Now the problem arises that int.Parse throws an OverflowException which I was not able to catch at the right point (it reoccurs on the line of the return statement for some reason and I can not handle it intelligently one level further up because it never arrives there).

The question is: Is there a pre-implemented comparer for this kind of thing? And what could be the reason that the exception turns up at funny places?

Calling code:

    IComparer fiComparer = new FileInfoSorter();

        try
        {
            Array.Sort(filesOfExtInfo, fiComparer);
        }
        catch (OverflowException ex)
        {
            // Do not know yet if I can use ex.Data in this way.
            WriteStatusLineAsync("Error: Encountered too large a version number on file: " + ex.Data["File"]);
        }

EDIT1: Int.Parse throws OverflowException when it encounters a too big number. It should not happen on a regular basis but I want it covered.

EDIT2: I ended up adjusting my own Comparer. Went away from int.Parse and just left-padded with zeroes for comparison. Code here:

    public class FileInfoSorter : IComparer
    {

        int IComparer.Compare(Object x, Object y)
        {
            FileInfo _x = x as FileInfo;
            FileInfo _y = y as FileInfo;

            Match m1 = RegExps.ExprFileVersion.Match(_x.FullName);
            Match m2 = RegExps.ExprFileVersion.Match(_y.FullName);
            if (m1.Success && m2.Success) // we have versioned files
            {

                string n1;
                string n2;
                n1 = m1.Groups[2].Value;
                n2 = m2.Groups[2].Value;

                string s1 = m1.Groups[1].Value;
                string s2 = m2.Groups[1].Value;

                int max = Math.Max(n1.Length, n2.Length);

                n1 = n1.PadLeft(max, '0');
                n2 = n2.PadLeft(max, '0');

                if (s1.Equals(s2)) // we have to compare the version
                    // which is now left-padded with 0s.
                {
                    return ((new CaseInsensitiveComparer()).Compare(n1, n2)); 
                }
                else // not the same base file name. So the version does not matter.
                {
                    return ((new CaseInsensitiveComparer()).Compare(_x.FullName, _y.FullName));
                }
            }
            else // not versioned
            {
                return ((new CaseInsensitiveComparer()).Compare(_x.FullName, _y.FullName));
            }
        }
    }
+1  A: 

Yes, there is. This question has already been answered here. You basically want to call the StrCmpLogicalW function through the P/Invoke layer. See the original answer for full details:

http://stackoverflow.com/questions/248603/natural-sort-order-in-c

casperOne
I don't like P/Invoke. It always makes me think it is some kind of hack ;)Thank you. I will have a look into it. Any thoughts on my exception problem?
Again, thank you.I ended up implementing my own working sorter.