NikTuk

ok, I need to store a list of key/value pairs of type string, and I need to be able to sort by either key or value, ascending and/or descending.

SortedList works ok by key, but when working by values, it's not that helpfull unless I revert key<->value values (not too efficient).

Is there a better way



Re: Visual C# General What's the most efficient way of doing this?

Wiltek Ghozali

How about if you use the DataTable, with key and value as the field.
A lot of things can you do with this data type.





Re: Visual C# General What's the most efficient way of doing this?

Navaneeth

NikTuk wrote:

ok, I need to store a list of key/value pairs of type string, and I need to be able to sort by either key or value, ascending and/or descending.


You can keep Key/Value pairs using NameValueCollection class. But it is not providing any way to sort the values. I think you can do this with a combination of arraylits and NameValueCollection classes.





Re: Visual C# General What's the most efficient way of doing this?

Tharmapalan

Hi Niktuk

you can do two ways

1- create datatable

2-create array with two prperty columns (in 2005 you can create with genertic class)

i would have discuss about first method

you a create table with two columns for keyvalue and key type

datatable table1=new datatable();

table1.columns("key",typeof(int));

table2.colums("type",typeof(string));

after created table add value to the rows afterwards u can ordered as descending and ascending order use by sql command.

table1.rows.add(kesyvalue,keytype)

i thing this method to be efficent rather than others

Regards

palan





Re: Visual C# General What's the most efficient way of doing this?

ChunSheng Tang - MSFT

Hi, Nik

Since there is no specific data type that is able to sort by both key and value, I suggest that you build your own one. I write a prototype for your reference.

Code Snippet

public class DKeyDictionary<TKey, TValue> : IDictionary<TKey, TValue>
where TKey : IComparable<TKey>
where TValue : IComparable<TValue>
{
private class KeyPair<TKey, TValue>
{
private TKey key;

public TKey Key
{
get { return key; }
set { key = value; }
}
private TValue value;

public TValue Value
{
get { return this.value; }
set { this.value = value; }
}

public KeyPair()
{
}
public KeyPair(TKey key, TValue value)
{
this.key = key;
this.value = value;
}
}

//Internal data store

private List<KeyPair<TKey, TValue>> data;

public DKeyDictionary()
{
data = new List<KeyPair<TKey, TValue>>();
}

public void SortByKey()
{
data.Sort(delegate(KeyPair<TKey, TValue> keyPair1, KeyPair<TKey, TValue> keyPair2) { return keyPair1.Key.CompareTo(keyPair2.Key); });
}
public void SortByValue()
{
data.Sort(delegate(KeyPair<TKey, TValue> keyPair1, KeyPair<TKey, TValue> keyPair2) { return keyPair1.Value.CompareTo(keyPair2.Value); });
}

public override string ToString()
{
StringBuilder s = new StringBuilder();
foreach (KeyPair<TKey, TValue> keyPair in data)
{
s.Append("(" + keyPair.Key + ", " + keyPair.Value + ")");
}
return s.ToString();
}

#region IDictionary<TKey,TValue> Members

public void Add(TKey key, TValue value)
{
data.Add(new KeyPair<TKey, TValue>(key, value));
}

////////////////////////////////////////////////////////

//My work ends here. Add the other members of the interfaces below.

///////////////////////////////////////////////////////

#endregion

}

The DKeyDictionary uses a KeyPair type field as its internal data store. It should be straightforword.

Usage:

Code Snippet

DKeyDictionary<string, string> dict = new DKeyDictionary<string, string>();

dict.Add("1", "D");

dict.Add("2", "C");

dict.Add("3", "B");

dict.Add("4", "A");

Console.WriteLine(dict.ToString());

dict.SortByValue();

Console.WriteLine(dict.ToString());

dict.SortByKey();

Console.WriteLine(dict.ToString());

Best Regards

Chunsheng Tang