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;
sortList.Add(1,2);
sortList.Add(2,5);

// 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?

Recommended Answers

All 11 Replies

sortList.Values.Min()

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

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>();
            sList.Add("1",1);
            sList.Add("2", 2);

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

or:

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

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(' ');
                        arrKey.Add(subKey.Key);
                       

                       
                        //get minimum
                        foreach(string mee in arrKey)
                        {                                                       
                            if(sortList3.ContainsKey(mee))
                            {                               
                              sortList3.TryGetValue(mee, out v);
                              sortList5.Add(mee, v);
                              result.Items.Add(v.ToString());  
                            }
                        }
                        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?

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

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);                   
                    sortList6.Add(first, last1);                                       
                }                   
                               
                    ArrayList arrKey = new ArrayList();
                    double v;
                    double min;                 
                   
                    foreach (KeyValuePair<string,double> subKey in sortList6)
                    {                      
                        arrKey.Clear();
                        sortList5.Clear();
                        
                        subKey.Key.Split(' ');
                        arrKey.Add(subKey.Key);                       
                       
                        //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;
                                      
                    }                                                                                
            }

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

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);                   
    sortList6.Add(first, last1);                                       
}                   
                               
ArrayList arrKey = new ArrayList();
double v;
double min;                 
                   
foreach (KeyValuePair<string,double> subKey in sortList6) {                      
    arrKey.Clear();
    sortList5.Clear();
                        
    subKey.Key.Split(' ');
    arrKey.Add(subKey.Key);                       
                       
    //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

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..

Change lines 16-17 to

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

This will split and store the keys. But it will still only contain keys for the current value from sortList6.

I had solved my problem, Thank you very much Momerath ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.