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 8858909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:56:03+00:00 2026-06-14T14:56:03+00:00

I have a sorted array of strings. Given a string that identifies a prefix,

  • 0

I have a sorted array of strings.
Given a string that identifies a prefix, I perform two binary searches to find the first and last positions in the array that contain words that start with that prefix:

string [] words = {"aaa","abc","abcd","acd"};
string prefix = "abc";
int firstPosition = Array.BinarySearch<string>(words, prefix);
int lastPosition = Array.BinarySearch<string>(words, prefix + char.MaxValue);
if (firstPosition < 0)
    firstPosition = ~firstPosition;
if (lastPosition < 0)
    lastPosition = ~lastPosition;

Running this code I get firstPosition and lastPosition both equal to 1, while the right answer is to have lastPosition equal to 3 (i.e., pointing to the first non-matching word).
The BinarySearch method uses the CompareTo method to compare the objects and I have found that

("abc"+char.MaxValue).CompareTo("abc")==0

meaning that the two string are considered equal!
If I change the code with

int lastPosition = Array.BinarySearch<string>(words, prefix + "z");

I get the right answer.
Moreover I have found that

("abc"+char.MaxValue)==("abc")

correctly (with respect to my needs) returns false.

Could you please help me explaining the behavior of the CompareTo method?

I would like to have the CompareTo method to behave like the ==, so that the BinarySearch method returns 3 for lastPosition.

  • 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-14T14:56:04+00:00Added an answer on June 14, 2026 at 2:56 pm

    According to the MSDN, string.CompareTo should not be used to check whether two strings are equal:

    The CompareTo method was designed primarily for use in sorting or alphabetizing operations. It should not be used when the primary purpose of the method call is to determine whether two strings are equivalent. To determine whether two strings are equivalent, call the Equals method.

    To get the behavior you wish, you could make use of the overload that accepts an IComparer<T>:

    int lastPosition = Array.BinarySearch<string>(words, prefix + char.MaxValue, 
                                                  StringComparer.Ordinal);
    

    This will return -4 for lastPosition as there is no string with that prefix in the array. I don’t understand why you expect 3 in that case…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1-)For sorted array I have used Binary Search. We know that the worst case
I have a sorted array of doubles (latitudes actually) that relatively uniformally spread out
I have sorted list of strings that I move between php and java. to
I have this array of strings private static String[] colorsArray = { #bde876, #ff8581,
I have an array of strings which is sorted by default. I want a
I've been given an array that needs to be sorted by its key (associative
I have an array of string, that look like this /var/folders/zy/zy4FzDGQEUuq9jZ1hTRFHk+++TI/-Tmp-/tarDir0172809001305545247/0516_GTB_HP_01.pdf /var/folders/zy/zy4FzDGQEUuq9jZ1hTRFHk+++TI/-Tmp-/tarDir0172809001305545247/0516_ETB_HP_28.pdf As you
Given an array of strings, I want to change every string into a string
I have an array that after being sorted appears like this: var arr =
I have a sorted mutable array of a class called Topic. The topics represent

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.