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

  • SEARCH
  • Home
  • 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 7845529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:15:18+00:00 2026-06-02T17:15:18+00:00

I have an array like this – string[] input = new string[] {bRad, Charles,

  • 0

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;

});
  • 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-02T17:15:20+00:00Added an answer on June 2, 2026 at 5:15 pm

    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)

    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    Array.Sort(input, delegate(string x, string y)
    {
        if (x == y)
            return 0;
    
        int shortestLength = Math.Min(x.Length, y.Length);
    
        for (i = 0; i < shortestLength; i++)
        {
            if (x[i] < 91 && y[i] < 91)
                return x.CompareTo(y);
            else if (x[i] < 91)
                return -1;
            else if (y[i] < 91)
                return 1;
        }
        return x.CompareTo(y);
    });
    
    stopWatch.Stop();
    double ms = (stopWatch.ElapsedTicks * 1000.0) / Stopwatch.Frequency;
    Debug.WriteLine("Optimized Time: " + ms);
    

    code to continue checking for Capital

    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    Array.Sort(input, delegate(string x, string y)
    {
        if (x == y)
            return 0;
    
        int xlen = x.Length;
        int ylen = y.Length;
        int longestLength = Math.Max(xlen, ylen);
    
        for (i = 0; i < longestLength; i++)
        {
            if (i < xlen && i < ylen && x[i] < 91 && y[i] < 91)
                return x.CompareTo(y);
            else if (i < xlen && x[i] < 91)
                return -1;
            else if (i < ylen && y[i] < 91)
                return 1;
        }
        return x.CompareTo(y);
    });
    
    stopWatch.Stop();
    double ms = (stopWatch.ElapsedTicks * 1000.0) / Stopwatch.Frequency;
    Debug.WriteLine("Optimized Time: " + ms);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have array defined like this : ArrayList<String> items = new ArrayList<String>(); I have
Lets say I have an array like this: string [] Filelist = ... I
I have an array like this: int[,] iMap = new int[iMapHeight, iMapWidth] { {
I have a array like this: my @arr = (Field3,Field1,Field2,Field5,Field4); Now i use map
i have an array input like this Array ( [col1] => Array ( [208]
I have an array like this, with missing keys: array(2) { [0]=> string(4) Bill
I have an array like this a = [] a << B.new(:name => c)
I have an array like this: pnotifyArr[] = array( type => New .$RowP[0].</br>, title
Let's say I have an array like this: string x[2][55]; If I want to
I have an array like this. int image[] = {R.drawable.d002_p001,R.drawable.d002_p002,R.drawable.d002_p003, R.drawable.d002_p004,R.drawable.d002_p005,R.drawable.d002_p006}; Right now I

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.