I have a large dataset for which sorting key calculation is quite expensive. What I would like to do is use the DSU template where I take the strings and calculate the sort key. Example:
Qty Name Supplier
Row 1: 50 Widgets IBM
Row 2: 48 Thingies Dell
Row 3: 99 Googaws IBM
Sorted by number of supplier and I might be the sort keys: 0050 IBM, 0048 Dell, 0099 IBM. The numbers are right-aligned, and the text is left-aligned, if necessary, everything is filled.
If I need to sort in descending order of quantum, I can simply subtract the value of the constants (for example, 10000) for creating sort keys: 9950 IBM, 9952 Dell, 9901 IBM.
How to quickly / cheaply build a top-down key for alphabetical fields in C #?
[My details are all 8-bit ASCII characters w / ISO 8859.]
Note. In Perl, this can be done using the padding bit of a string :
$subkey = $string ^ ( "\xFF" x length $string );
Porting this solution directly to C # does not work:
subkey = encoding.GetString(encoding.GetBytes(stringval).
Select(x => (byte)(x ^ 0xff)).ToArray());
I suspect due to differences in how strings are processed in C # / Perl. Perhaps Perl is sorted in ASCII order and C # is trying to be smart?
Here is an example of a piece of code that tries to execute this:
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
List<List<string>> sample = new List<List<string>>() {
new List<string>() { "", "apple", "table" },
new List<string>() { "", "apple", "chair" },
new List<string>() { "", "apple", "davenport" },
new List<string>() { "", "orange", "sofa" },
new List<string>() { "", "peach", "bed" },
};
foreach(List<string> line in sample)
{
StringBuilder sb = new StringBuilder();
string key1 = line[1].PadRight(10, ' ');
string key2 = line[2].PadRight(10, ' ');
key2 = encoding.GetString(encoding.GetBytes(key2).
Select(x => (byte)(x ^ 0xff)).ToArray());
sb.Append(key2);
sb.Append(key1);
line[0] = sb.ToString();
}
List<List<string>> output = sample.OrderBy(p => p[0]).ToList();
return;