## leo88

hi there, i got some problems in extract the minimum value in a sortedlist, as below:

``````Dictionary<string, double> sortList = new Dictionary<string, double>();
double min;

// min = sortList5.Min(x => x.Value);``````

I wants to find minimum value of the sortlist.value, is there has any functions of sortedlist/dictionary to find minimum/lowest value in such problem? I saw from online got a method " min = sortList5.Min(x => x.Value);", but there had errors when i excecuted.. Anyone knows?

## Momerath 1,327

``sortList.Values.Min()``

And why do you call it a sortList when it is neither sorted nor a list?

## jbrock31 11

momerath's answer definitely works, but i didn't have a problem using your code, although i altered it slightly.

``````Dictionary<String,Double> sList = new Dictionary<string, double>();

double min;
min = sList.Min(m => m.Value);
if (min != 0)
MessageBox.Show(min.ToString());``````

## Mitja Bonca 557

or:

``double min = sortList.Min(x => x.Value);``

## leo88

I tried those two ways and also can works, but i some problem with my sortedlist, can help me check?

``````foreach (KeyValuePair<string,double> subKey in sortList6)
{
arrKey.Clear();
sortList5.Clear();

subKey.Key.Split(' ');

//get minimum
foreach(string mee in arrKey)
{
if(sortList3.ContainsKey(mee))
{
sortList3.TryGetValue(mee, out v);
}
}
min = sortList5.Min(x => x.Value);
if(min>=subKey.Value)
{
result.Items.Add(subKey.Key.ToString() + " " + subKey.Value.ToString());
}
}``````

Sortlist5 inside the nested foreach seems like get no any value from sorlist3, was my way of coding wrong?

## leo88

Oh ya, result means the name of listbox, i am using window form Visual basic..

## leo88

ermm, sorry i post wrong, watch below :

``````foreach(string cs in Comset.Items)
{
String first = cs.Substring(cs.IndexOf("(") + 1, cs.IndexOf(")") - cs.IndexOf("(") - 1);
String last = cs.Substring(cs.IndexOf(")") + 1);
double last1 = Convert.ToDouble(last);
}

ArrayList arrKey = new ArrayList();
double v;
double min;

foreach (KeyValuePair<string,double> subKey in sortList6)
{
arrKey.Clear();
sortList5.Clear();

subKey.Key.Split(' ');

//get minimum
foreach(string mee in arrKey)
{
if(sortList3.ContainsKey(mee))
{
sortList3.TryGetValue(mee, out v);
sortList5.Add(mee, v);   //sortlist5 doesnt pass the value
}
}

min = sortList5.Min(x => x.Value);

if (min >= subKey.Value)
{
result.Items.Add(subKey.Key.ToString() + " " + subKey.Value.ToString());
}

else
continue;

}
}``````

## leo88

i call its sortlist for temporary only,i will change it when i finish the project..

## Momerath 1,327

I've formatted your code since your current indentations might lead you to believe something else is happening.

``````foreach(string cs in Comset.Items) {
String first = cs.Substring(cs.IndexOf("(") + 1, cs.IndexOf(")") - cs.IndexOf("(") - 1);
String last = cs.Substring(cs.IndexOf(")") + 1);
double last1 = Convert.ToDouble(last);
}

ArrayList arrKey = new ArrayList();
double v;
double min;

foreach (KeyValuePair<string,double> subKey in sortList6) {
arrKey.Clear();
sortList5.Clear();

subKey.Key.Split(' ');

//get minimum
foreach(string mee in arrKey) {
if(sortList3.ContainsKey(mee)) {
sortList3.TryGetValue(mee, out v);
sortList5.Add(mee, v);   //sortlist5 doesnt pass the value
}
}

min = sortList5.Min(x => x.Value);

if (min >= subKey.Value) {
result.Items.Add(subKey.Key.ToString() + " " + subKey.Value.ToString());
} else {
continue;
}
}``````

Line 16 : This line does nothing. Split() returns an array, it doesn't modify the string you are working on (repeat after me: Strings are immutable. Any operation on a string returns a new string, it doesn't modify the string I'm working with).
Line 20 : Because of line 13 there will only ever be one value in arrKey
Line 14 : Because of this line, there will be a most 1 value in sortList5. If the current key from sortList6 is *not* in sortList3, sortList5 will be empty.
Line 32 : You are already at the end of the loop. Putting 'continue' here does nothing at all.

commented: Great work is done here. +9

## leo88

The key in sortlist6 is something like that => 1 2 or 1 2 3, so i wan to split it become
1
2
after that, i store those keys to arraylist(line 20), so arrKey should have 2 keys.
line 14: i use .clear() because for every next loop in the outside most foreach,i wants to store new key for the arrKey, and same for the sortList5..

## Momerath 1,327

Change lines 16-17 to

``````String[] keys = subKey.Key.Split(' ');
foreach (String item in keys) {