Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7839255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:23:12+00:00 2026-06-02T15:23:12+00:00

I have an array which I want to sort using some custom logic. new

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:23:14+00:00Added an answer on June 2, 2026 at 3:23 pm

    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:

    public class CapitalizerComparer : IComparer<string>
    {
        public int Compare(string x, string y)
        {
            int[] xCount = new int[26];
            int[] yCount = new int[26];
    
            foreach(char c in x)
            {
                if (char.IsUpper(c))
                    xCount[c-'A']++;
            }
    
            foreach (char c in y)
            {
                if (char.IsUpper(c))
                    yCount[c-'A']++;
            }
    
            for (int i = 0; i < xCount.Length; i++)
            {
                if(xCount[i] > yCount[i])
                    return -1;
                else if(yCount[i] > xCount[i])
                    return 1;
            }
            return x.CompareTo(y);
        }
    }
    

    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).

    //use case:
    var input = new string[] { "bRad", "Charles", "sam", "lukE", "vIctor" };
    Array.Sort(input, new CapitalizerComparer());
    

    Output:

    Charles
    lukE
    vIctor
    bRad
    sam
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have some pointer, which I want to reinterpret as static dimension array
I have an array of objects which I want to sort based on one
In a PHP project I have some data I want to sort using a
I have an array of Paths which i want to read out with Template
I have an array of file names which I want to download. The array
i have an char array b[20] which i want to write into a file
Consider I have an array of elements out of which I want to create
I have a collection (or list or array list) in which I want to
I have a UITableView which I want to populate with details from an array
I have a huge text file which I want to explode into an array.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.