hi to all
what is a sorted list ,how it is sorted while adding the element to sortedlist,
can we add class objects to this sorted list
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nilanka DharmadasaPosted Dec 24, 2009, 2:25 AM
YOu can give objects for keys. But you should implement that class from Ico mpareble interface . I changed your code. Now it is working.
public class A : IComparable
{
public int id;
public string name;
public A()
{
}
public A(int id, string name)
{
this.id = id;
this.name = name;
}
public int CompareTo(object obj)
{
if (obj is A)
{
A p2 = (A)obj;
return name.CompareTo(p2.name);
}
else
throw new ArgumentException("Object is not of type A.");
}
}
public class B
{
public int id1;
public string name1;
public B()
{
}
public B(int id, string name)
{
this.id1 = id;
this.name1 = name;
}
}
static class Program
{
///
/// The main entry point for the application.
///
///
[STAThread]
static void Main()
{
SortedList mysl = new SortedList();
A a = new A(5, "srinivas");
A a1 = new A(2, "chintu");
B b = new B(3, "dfgsrinivas");
B b1 = new B(6, "dschintu");
mysl.Add(a, b);
mysl.Add(a1, b1);//here iam getting an invalidoperation exception
for (int i = 0; i < mysl.Count; i++)
{
Console.WriteLine("\t{0}", mysl.Keys[i].name);
}
}
}
I wrote the compare to method so that it comares the name. So this will sort the objects by the name of A object.
You can write it as you like.
Please do not forget to tick 'Do you like this answer' checkbox if this answer helps you.
srinivasPosted Dec 24, 2009, 7:18 AM
i have some problem with Dictionary can you help me to solve my doubt
key in dictionary should have a gethashcode why this gethashcode how it is help full for Dictionary collection
The implementation of GetHashCode() must satisfy these requirements:
->The same object should always return the same value.
->Different objects can return the same value.
->It should execute as quickly as possible; it must be inexpensive to compute.
->It must not throw exceptions.
->It should use at least one instance field.
->The hash code value should be evenly distributed across the entire range of numbers that an int can store.
->At best, the hash code should not change during the lifetime of the object.
can you explain about these points
please hope you solve my doubt
thank you
Nilanka DharmadasaPosted Dec 24, 2009, 3:49 AM
srinivasPosted Dec 24, 2009, 2:59 AM
srinivasPosted Dec 24, 2009, 12:25 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace sortedlist2
{
public class A
{
public int id;
public string name;
public A()
{
}
public A(int id, string name)
{
this.id = id;
this.name = name;
}
}
public class B
{
public int id1;
public string name1;
public B()
{
}
public B(int id, string name)
{
this.id1 = id;
this.name1 = name;
}
}
class Program
{
static void Main(string[] args)
{
SortedList<A, B> mysl = new SortedList<A, B>();
A a = new A(5, "srinivas");
A a1 = new A(2, "chintu");
B b = new B(3, "dfgsrinivas");
B b1 = new B(6, "dschintu");
mysl.Add(a, b);
mysl.Add(a1, b1);//here iam getting an invalidoperation exception
}
}
}
if we add an object to key then how it sorts the key values
Hiren SoniPosted Dec 24, 2009, 12:08 AM
in sortedlist key and item both are of a object type. You can store any class object in it.
srinivasPosted Dec 23, 2009, 11:48 PM
and what are the advantages and disadvantages of sortedlist over list
Hiren SoniPosted Dec 23, 2009, 11:38 PM
whenever you insert item in list. It inserted according by key in list.
list.add(2,"H")
list.add(1,"r")
so in list it become
1 r
2 H
srinivasPosted Dec 23, 2009, 11:37 PM
can you give a example and how the keys are sorted while inserting that means what is the mechanism
thank you
Hiren SoniPosted Dec 23, 2009, 11:31 PM
the collection sorted according by keys.
slist.add(Key,item)
where key is the key of item and item is the any item or object. All items are inserted acording to their keys. Each key must be unique. Adding a duplicate key throw exception.