Click here to Skip to main content
15,918,193 members
Please Sign up or sign in to vote.
2.00/5 (3 votes)
See more:
I am new to programming in C#. Following program gives incorrect value if BinarySerach method is used to serach a string in the array list. can somebody explain what is the problem in this program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace CollectionDirectories
{
    class sortArrayList
    {
        static int sr_no;
        static void Main()
        {
            
            ArrayList al = new ArrayList();
            al.Add("Hello");
            al.Add("World");
            al.Add("this");
            al.Add("is");
            al.Add("yes");
            al.Add("a");
            al.Add("test");
            al.Remove("test");
            al.Insert(4,"not");
// comment on output: In this list index of "World" is 1 but out put is different.
            Console.WriteLine("First Search ="+al.BinarySearch("World"))
Console.WriteLine("_______________");
            al.Sort();
            foreach (object s in al)
                Console.WriteLine(s.ToString());f
// comment on output: If reverseSort is used the output after reverse is length of arrarylist +1
            al.Sort(new reverseSort());
            Console.WriteLine("after reverse____________");
            foreach (object s in al)
            {
                sr_no++;
                Console.WriteLine(sr_no + ":" + s.ToString());
            }
            Console.WriteLine("sr. of word_______________");
          
            Console.WriteLine("Second Search = "+ al.BinarySearch("World"));
            Console.WriteLine("____________");
            foreach (object s in al)
                Console.WriteLine(s.ToString());
          
            Console.Read();
        }
        static int i;
        public class reverseSort : IComparer
        {
            int IComparer.Compare(Object x, Object y)
            {
                i++;
                Console.WriteLine(i+&" "+ x + " "+ y);
                  
                return ((new CaseInsensitiveComparer()).Compare(y, x));
            }
        }
    }
}
Posted
Updated 10-Feb-10 7:28am
v2

Did you read the documentation?


Rajiv Yelkawar wrote:
// comment on output: In this list index of "World" is 1 but out put is different.

The first time that you search the list you haven't sorted it yet at all so of course the search is going to give screwey results.


Rajiv Yelkawar wrote:
// comment on output: If reverseSort is used the output after reverse is length of arrarylist

The second time you search you're doing it after sorting the list backwards so again, the search will fail.

The list needs to be sorted with in INCREASING order for the search to work.
 
Share this answer
 
Actually, it gives you exactly what the documentation tells you it will give you (after you fix the compile errors). The problem is that the BinarySearch method expects things to be sorted is ascending order or else the search won't work. You're sorting the list in descending order and then searching. Please refer to the documentation[^].
 
Share this answer
 
Dear Jimmanual,
Thanks for quick and appropreate reply. As you can see in the code I have created a public class reverseSort implementing IComparer. Program gives incorrect output at first search if I comment "al.Sort(new reverseSort());" in the code and correct output at second search and vice versa. I regreat for not putting this in my original question. Nevertheless thanks.
 
Share this answer
 
Dear Jimmanual,
Thanks, I have gone through the document. Your answer is of immense help to me. Thanks again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900