-5

i am working on assignment where i define following dictionary

Dictionary<string, List<fileInfo>> myDic = new Dictionary<string, List<fileInfo>>();

where

public class fileInfo
    {
        public string fileFullName;
        public double tf;
        public double idf;
        public double tf_idf;
        public double cosineSim;

        public fileInfo()
        {
            fileFullName = null;
            tf = 0.0;
            idf = 0.0;
            tf_idf = 0.0;
            cosineSim = 0.0;
        }
    }

now i want to sort the list with respect to COSINESIM

Taimur Adil
  • 77
  • 1
  • 10

2 Answers2

1

If you want to get a combined (merged) list of fileInfo sorted by cosineSim you can use Dmitry's answer.

Else if you want to order the items in each list in your dictionary you can use

myDic.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.OrderBy(fi => fi.cosineSim).ToList());
serdar
  • 1,564
  • 1
  • 20
  • 30
0

If you want to get a combined (merged) list of fileInfo sorted by cosineSim you can do

  List<fileInfo> result = myDic.Values
    .SelectMany(item => item)        // amalgamating all the lists   
    .OrderBy(item => item.cosineSim) // ordering the result
    .ToList();    

In case you want to sort each list within myDic dictionary, just Sort them:

  foreach(var list in myDic.Values)
    list.Sort(Comparer<fileInfo>.Create((left, right) => 
      left.cosineSim.CompareTo(right.cosineSim)));
serdar
  • 1,564
  • 1
  • 20
  • 30
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • I really thank full "my Teacher": i work on it for last connective 5 to 6 hr but all in vain.... you really solved my problem – Taimur Adil Feb 02 '15 at 08:16