Sandra
Sandra

Reputation: 3750

Dictionary or List

I have strings associated with some double value. I need to be able to sort them easily by the value and to easily get the strings as some kind of list. There could be 100k+ of those pairs.

So, my question is whether I should use a Dictionary with strings as keys and doubles as values or a List of KeyValuePairs with the same keys and values?

In case of the dictionary it's easy to get the keys as a list via

dict.Keys.toList()

and in case of the list it's easy to sort by value via

list.Sort(delegate(KeyValuePair x, KeyValuePair y) { return y.Value.CompareTo(x.Value); }).

I haven't found a way to do both though. What do you recommend?

Upvotes: 4

Views: 1007

Answers (5)

deepee1
deepee1

Reputation: 13196

here's 1 way to select all of the strings in your KeyValue list with a value of 1 or to select all of the strings from the keyvaluepair

List<string> onestrings = list.Where(a=>a.Value == 1).Select(a => a.Key).ToList();
List<string> allstrings = list.Select(a => a.Key).ToList();

Upvotes: 1

spender
spender

Reputation: 120440

How about a Lookup?

Dictionary<string,double> dic = new Dictionary<string,double>();
ILookup<double,string> lookup = dic.ToLookup(kvp => kvp.Value, kvp => kvp.Key);

Upvotes: 0

Dan Tao
Dan Tao

Reputation: 128317

I would recommend a SortedList<double, string>. This sounds like exactly what you want:

  • Automatically sorted by the double values (the Keys property)
  • The strings are accessible in order via the Values property

This will only work if your double values are unique, of course. Otherwise, you might wrap a SortedList<double, List<string>> in your own collection, something like:

class DoubleStringList
{
    SortedList<double, List<string>> _strings = new SortedList<double, List<string>>();

    public void Add(string str, double value)
    {
        List<string> list;
        if (!_strings.TryGetValue(value, out list))
        {
            _strings[value] = list = new List<string>();
        }

        list.Add(str);
    }

    public IEnumerable<KeyValuePair<double, string>> GetEntries()
    {
        var entries = from entry in _strings
                      from str in entry.Value
                      select new KeyValuePair<double, string>(entry.Key, str);

        return entries;
    }
}

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564403

One main consideration is whether your values are unique. If they are not, a Dictionary will not work, as it requires unique keys. It will also be more difficult to sort.

If you're just using this to store pairs of values, and there are no uniqueness constraints, I'd personally use a List<Tuple<double,string>>.

Upvotes: 1

Matt Ellen
Matt Ellen

Reputation: 11592

I am assuming you have more than one string for a given double value.

You could still do it the other way: Dictionary<double, list<string>>

So you would take the double value as the key, and when you get a string with the same double value you add it to the list.

This way you get the look up speed of the dictionary, and you can still do a sort of the keys when you need to.

Upvotes: 0

Related Questions