1

I have a file with contains as
A,15
B,67
C,45
D,10

I am reading the data from file, but i wanted to read the data into a dictionary or hashtable but the data sould be sorted into it by its value that is
B,67
C,45
A,15
D.10

If Any other List will work as efficient manner, please suggest

Thanks

usr021986
  • 3,421
  • 14
  • 53
  • 64

3 Answers3

5

A Dictionary<,>/Hashtable has no defined sort; that will not work. A SortedDictionary<,> is sorted by key, not by value, so that won't work. Personally, I think you should just use a regular List<T> (for some simple T with the two properties), and after loading it:

list.Sort((x,y) => y.SecondProp.CompareTo(x.SecondProp));

the subtle x/y switch in there achieves "descending". If you also need the data keyed by the first property, then separately store a Dictionary<string,int>.

Full example:

class Program
{
    static void Main()
    {
        List<MyData> list = new List<MyData>();
        // load the data (replace this with a loop over the file)
        list.Add(new MyData { Key = "B", Value = 67 });
        list.Add(new MyData { Key = "C", Value = 45 });
        list.Add(new MyData { Key = "A", Value = 15 });
        list.Add(new MyData { Key = "D", Value = 10 });
        // sort it
        list.Sort((x,y)=> y.Value.CompareTo((x.Value)));
        // show that it is sorted
        foreach(var item in list)
        {
            Console.WriteLine("{0}={1}", item.Key, item.Value);

        }
    }
}

internal class MyData
{
    public string Key { get; set; }
    public int Value { get; set; }
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

Or, use IComparable<>

Full Example:

 public class Program
    {
        public static void Main(string[] args)
        {
            List<MyData> list = new List<MyData>();
            // load the data (replace this with a loop over the file)    
            list.Add(new MyData { Key = "B", Value = 67 });
            list.Add(new MyData { Key = "C", Value = 45 });
            list.Add(new MyData { Key = "A", Value = 15 });
            list.Add(new MyData { Key = "D", Value = 10 });

            list.Sort();           
        }
    }


    internal class MyData : IComparable<MyData>
    {
        public string Key { get; set; }
        public int Value { get; set; }
        public int CompareTo(MyData other)
        {
            return other.Value.CompareTo(Value);
        }

        public override string ToString()
        {
            return Key + ":" + Value;
        }
    } 
Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
0

Are you sure you need to store it in a Dictionary? Usually, when you use a dictionary you need fast access to an item given a key, but you are not too much concerned about the internal ordering. So you may need to re-think your data structure.

Anyway, if you want to access the data in a dictionary ordered by value you can use a LINQ query for that:

Dictionary<string, int> data = new Dictionary<string, int>();
data.Add("A", 15);
data.Add("B", 67);
data.Add("C", 45);
data.Add("D", 10);

var ordered = (from d in data
                orderby d.Value
                select new Tuple<string, int>(d.Key, d.Value));

foreach (var o in ordered)
    Console.WriteLine(o.Item1 + "," + o.Item2);            
Pescuma
  • 2,044
  • 1
  • 12
  • 5