0

I have read questions related to sorting an array of objects by property, but I have a special case where I need the aditional StringComparer.Ordinal object to perform a custom sort.

I have a DirectoryInfo[] array where I need the subdirectories to be sorted by their FullName property but I need the sorting order of the numbers contained in the paths to be:

1 - 2 - 4 - 10 - 14 - 25

instead of

1 - 10 - 14 - 2 - 25 - 4

(I have paths such as c:\Folder\Subfolder\1, c:\Folder\Subfolder\2 ...)

I think this would be acomplished with: Array.Sort(paths, StringComparer.Ordinal) if my array was an array of strings.

How can I acomplish this if my array is an array of DirectoryInfo[]? I would like to avoid extending DirectoryInfo by creating a new class.

Jorge
  • 1,350
  • 2
  • 10
  • 19
  • Create custom `IComparer` that would compare using `DirectoryInfo.FullName` and `StringComparer.Ordinal`? – Dmytro Marchuk Mar 17 '16 at 15:14
  • For that I would have to create a class that extends DirectoryInfo and implements IComparer, wouldnt I? Is there another option? – Jorge Mar 17 '16 at 15:15
  • No, it would just implement `IComparer`. Notice, it's not `IComparable` but `IComparer`. Precisely, you want `IComparer`, and then use `Array.Sort (T[], IComparer)` – Dmytro Marchuk Mar 17 '16 at 15:17
  • Well the duplicate marker only solves half of your problem. The other would be: `directoryInfoArray.OrderBy(s => s.FullName, MyComparer);` – Manuel Zelenka Mar 17 '16 at 15:28

0 Answers0