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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 23, 2009, 9:25 PM EST
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.
Hiren SoniRank #694 · 3.5K reputationPosted Dec 23, 2009, 6:31 PM EST
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.
srinivas137 reputationPosted Dec 23, 2009, 6:37 PM EST
can you give a example and how the keys are sorted while inserting that means what is the mechanism
thank you
Hiren SoniRank #694 · 3.5K reputationPosted Dec 23, 2009, 6:38 PM EST
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
srinivas137 reputationPosted Dec 23, 2009, 6:48 PM EST
and what are the advantages and disadvantages of sortedlist over list
Hiren SoniRank #694 · 3.5K reputationPosted Dec 23, 2009, 7:08 PM EST
in sortedlist key and item both are of a object type. You can store any class object in it.
srinivas137 reputationPosted Dec 23, 2009, 7:25 PM EST
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
srinivas137 reputationPosted Dec 23, 2009, 9:59 PM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Dec 23, 2009, 10:49 PM EST
srinivas137 reputationPosted Dec 24, 2009, 2:18 AM EST
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