I have a program where I am parsing sql scripts in order of the file directory. The idea from the team was that changes or additions to the sql scripts were done in order, and so the folders are named from 0 to 12. So I need to parse through these folders in numerical order, however when they are parsed in order, these are the order in which I am placing them as the Key in the dictionary:
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\0
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\1
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\10
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\11
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\12
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\2
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\3
etc...
When I iterate through this dictionary, I want to run these folders in numerical order so I can build my sql scripts in the order they were designed. I have the path saved as the key (string), and I need to reorganize them so that the path folders are listed in numerical order. So that they look like this:
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\0
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\1
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\2
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\3
etc...
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\9
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\10
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\11
C:\Dev\FileResearch\ParseSqlConsole\DbScriptFolders\12
My Dictionary structure is in the form of <string, List<string>>. I am looking at a file directory that holds a series of folders, and each folder holds a handful of SQL files for building a database. My Dictionary Keys are the folder paths for the subfolders inside this directory, and the values is a List of strings that hold the paths for those files inside the folder path in the Key. I'll edit my question to mention this. How can I order my dictionary by my keys in numerical order?
So I've learned a lot from some of the comments, and it helped guide me to this answer. The answer to what I needed relies on the following points:
A Dictionary cannot be ordered. To store the key value pairs in a desired order, a
StoredDictionaryneeds to be usedNatural Sort is not something that is handled easily. To achieve this, the
Linqpackage needs to be used with theOrderBy()function..OrderedBy()needs to use aIComparerand in this case, a custom one had to be designedI was very fortunate to come across an article that makes a custom comparer specifically for natural ordering of strings with numbers in them. A special thanks to James McCormack for this comparer.
First off, I've turned my
scriptsPathsdictionary into a SortedDictionary. And I also instantiate aorganizedPathsSortedDictionary.Then, once my program has parsed the directories for the subdirectory paths, I then created a new class, named
NaturalSortComparer, where I've placed the customer IComparer found from the link above:With the custom comparer in place, I now used the
.OrderBy()with the custom comparerAfter a foreach loop to Console.WriteLine() the keys, I get the order in which I wanted:
A final note Every resource I found on
SortedDictionarystates that they are much more resource intensive than aDictionary, so watch out with very large ones. However, I can only hope that my folder structure will not grow much more, so this is acceptable for me.