I would like to sort an array of strings in a directory, given a custom mapping (it’s actually a sorting of stock names based on their sector). I am unsure of what data structures to use to represent the mapping, and how to write the custom sort method.
So for instance, suppose I had the following string array:
string[] fileNames = "bac.csv", "c.csv", "cvx.csv", "java.csv", "msft.csv", "xom.csv";
And here are the mappings:
{"bac", "c"} => 0
{"msft", "java"} => 1
{"xom", "cvx"} => 2
I would like string[] customSort(string[] fileNames) to return the following:
"bac.csv", "c.csv", "java.csv", "msft.csv", "xom.csv", "cvx.csv"
What data structure would you use to represent the mappings, and what’s an elegant way of writing the sort method?
Array.Sort allows you to specify an array of keys, so you can do something like…
Just modify the
keys[i] = -1;statement to get the proper value from your mappings given thetokenvariable.