I have an array which I want to sort using some custom logic.
new string[] {"bRad", "Charles", "sam", "lukE", "vIctor"}
Now I want to sort this according to positional occurrence of capital letters in a string. If the first letter is capital then ignore others. If two strings have capital letters at same position then sort them alphabetically. If there are no capital letters in the string then it obviously goes to the end of the list. Performance is a key factor, there’s going to be huge amount data on which this is tested.
Output should be
new string[] {"Charles", "vIctor", "bRad", "lukE", "sam"}
Explanation:
Charles comes first because it has capital letter at first position.
vIctor comes second because it has capital letter at second position
bRad comes third because it has capital letter at second position but comes after I
lukE comes first because it has capital letter at fourth position
sam comes last because there are no capital letters in any position.
I am restricted to .NET 2.0. Please help.
Here is an approach for performance: You can use a custom comparer that follows the capitalization rules. For speed you can use two integer arrays each the size of the alphabet that you are using (26 different upper case characters in the simple case of ASCII) that keep track of character counts of capitalized letters, if the count for all the capitalized letters found are equal for both words you can just compare the strings themselves:
Since according to your rules the position of the capitalized character is irrelevant you have to look at all characters of both words to make a decision. Above algorithm hence should be optimal and O(n+m).
Output: