I have an array like this –
string[] input = new string[] {"bRad", "Charles", "sam", "lukE", "vIctor"}
Now I wanted to sort this according to position of capital letter occurring in each string. The first occurrence is the only occurrence to consider while sorting. If two strings have CAPs at same position then sort them alphabetically, the same applies to strings which does not have any CAPs, sort them alphabetically.
What I have done so far is not performing well enough. I have tried countless times to improve it but with no luck. There’s going to be huge amount data on which this is tested. So performance is of foremost importance. I’m using .NET 2.0 and I’m not allowed to use any higher versions.
public static int q, p, i, s;
public static Dictionary<string, int> a = new Dictionary<string, int>();
Array.Sort(input, delegate (string x, string y) {
if (x == y)
return 0;
if (a.TryGetValue(x + "|" + y, out s))
return s;
if (a.TryGetValue(y + "|" + x, out s))
return -s;
q = x.Length;
p = y.Length;
for (i = 0; i < x.Length; i++)
{
if (x[i] < 91)
{
q = i;
break;
}
}
for (i = 0; i < y.Length; i++)
{
if (y[i] < 91)
{
p = i;
break;
}
}
if (q == x.Length && p == y.Length)
s = x.CompareTo(y);
else if (q > p)
s = 1;
else if (q < p)
s = -1;
else
s = x.CompareTo(y);
a.Add(x + "|" + y, s);
return s;
});
Removing the dictionary for your cache alone sped it up (my example of 15000 values, with up to 500 chars per value) went from 2449.51ms with the dictionary, and after removing that went down to 58.72ms
I tried “craigmj”‘s idea of caching the individual values position which is faster then doing the concat but it seems with my random data no cache was still faster.
Here is some code to test out… this runs in 30ms compared to 2559ms (original)
code to continue checking for Capital