C# Sorted List by Value with Object

前端 未结 9 920
失恋的感觉
失恋的感觉 2021-01-21 20:06

I\'m trying to create an \"ordered\" cache of objects in C#, where the order is determined by how many times that has been accessed.

I\'ve looked into Dictionary, Sorted

9条回答
  •  -上瘾入骨i
    2021-01-21 20:37

    Building upon your pseudo code, this seems to be working:

    var MyCache = new Dictionary
    {
        {"My result 1", new Result("My result 1")},
        {"My result 2", new Result("My result 2")},
        {"My result 3", new Result("My result 3")},
        {"My result 4", new Result("My result 4")}
    };
    
    MyCache["My result 2"].IncreaseHits();
    MyCache["My result 2"].IncreaseHits();
    MyCache["My result 3"].IncreaseHits();
    
    foreach (var result in MyCache.OrderByDescending(x => x.Value.Hits))
    {
        Console.WriteLine(result.Value.Name + " - hits " + result.Value.Hits);
    }
    

提交回复
热议问题