Note that there are some explanatory texts on larger screens.

plurals
  1. POIComparer problem + How do I sort an array of strings naturally (FILE_10 > FILE_2) in .NET?
    primarykey
    data
    text
    <p>SOLVED at the bottom of my post.</p> <p>Or more specifically:</p> <p>I have a bunch of FileInfo objects (I need the FileInfo objects to exclude hidden, system and reparse point files).</p> <p>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.</p> <p>I have implemented a comparer:</p> <pre><code>/// &lt;summary&gt; /// 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. /// &lt;/summary&gt; 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 &amp;&amp; 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 &gt; 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)); } } } </code></pre> <p>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).</p> <p>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?</p> <p>Calling code:</p> <pre><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"]); } </code></pre> <p>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.</p> <p>EDIT2: I ended up adjusting my own Comparer. Went away from int.Parse and just left-padded with zeroes for comparison. Code here:</p> <pre><code> 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 &amp;&amp; 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)); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload