3

Possible Duplicate:
Natural Sort Order in C#

i have been looking around for some code to sort my files by name the same as windows,

My dir contains files "SD-Patch-1.sql", "SD-Patch-2.sql" ..., "SD-Patch-10.sql" this is how windows formats them but in my application when sorting by name it sorts them

SD-Patch-1.sql
SD-Patch-10.sql
... to 19
SD-Patch-2.sql

how do I get the same sort as windows to get the above im using

FileInfo[] files = dirInfo.GetFiles();
Array.Sort(files, (f1, f2) => f1.Name.CompareTo(f2.Name));
Community
  • 1
  • 1
HotHeadMartin
  • 260
  • 2
  • 10
  • 4
    Who else read this question and thought 'Easy bit of linq should do the job' then got here to find it was more complex? – Lloyd Powell May 14 '12 at 11:53
  • http://stackoverflow.com/questions/248603/natural-sort-order-in-c-sharp – Tim Schmelter May 14 '12 at 11:53
  • http://stackoverflow.com/questions/1012985/how-would-i-sort-a-list-of-files-by-name-to-match-how-windows-explorer-displays – bjornruysen May 14 '12 at 11:54
  • personally i rename my files to have the leading zeros in them, but surely a nifty comparer could do the trick. – ericosg May 14 '12 at 11:54
  • http://www.codeproject.com/Articles/11016/Numeric-String-Sort-in-C – Nikhil Agrawal May 14 '12 at 11:59
  • is there no way though Linq do i have to all the Windows API to sort them i thought there would be a way though Linq that i jsut could not think off? – HotHeadMartin May 14 '12 at 12:03
  • A very simple (but brittle) approach is to do: `var files = dirInfo.EnumerateFiles().OrderBy(x => int.Parse(x.Name.Replace("SD-Patch-", ""))).ToList();` – nawfal Jun 28 '14 at 19:26

1 Answers1

-1

You should use custom comparer in the sort parameter

list.Sort(new customComparer());

the Comparer should look something like this:

class customComparer: IComparer<Object>
{
  public int Compare(Object obj1, Object obj2)
  {
    //implament your own code for sorting any way you want
  }
}

One example of code for your case could be as fallow.

Create dictionary mapping each later to a corresponding number, something like this:

//Mapping between file name characters and their corresponding number of choice
Dictionary<char, int> alphaNumDict = new Dictionary<char, int>()
                                                  {
                                                      {'a',1},
                                                      {'b',2},
                                                      {'c',3},
                                                      {'d',4},
                                                      {'e',5}
                                                      ...
                                                  };

Then you can use a simple method to change string to a number and compare the strings' values:

//iterate over the string and convert it to a corresponding number
double sum =0;
for (int i = 0; i < myString.Length; i++)
{
    sum += alphaDict[myString[i]] * Math.Pow(10, i);
}

Now all you need to do is compare the number you get and return which is bigger

choppy
  • 739
  • 1
  • 12
  • 22
  • That's obvious thing. He has to create his custom comparer. What matters is what should be written here (`//implement your own code for sorting any way you want`) and what you write. – Nikhil Agrawal May 14 '12 at 12:06
  • You are right, i added code suggestion. – choppy May 15 '12 at 11:37