-1

I have the following data that its retrieved from two textbox. I would like to Sort it Descending by Time. I tried using a Dictionary but I cant insert duplicate values. Any Ideas?

ID     time                             ID       time
4       10                              15        19       
12      13      WANT SORTED BY TIME     12        13
15      19      ---->>                  12        13
4       10                              4         10 
12      13                              4         10
4

3 回答 3

6

字典不允许重复键,所以这不是你想要的,因为你的ID值显然不是唯一的键值。

创建一个类来保存您的数据,然后使用 Linq 的 OrderByDescending 对其进行排序:

public class MyTimeData {
   public int ID { get; set; }
   public int Time { get; set; }
}

var list = new List<MyTimeData>();
// Add items to the list
list = list.OrderByDescending(d => d.Time).ToList();
于 2013-08-13T18:39:26.407 回答
0

另一个选项,如果 LINQ 不可用或您感兴趣的东西,是SortedList数据结构(您可以反转顺序以使其降序,因为它默认为升序),您可以像这样使用它:

public class MyTimeData {
    public int ID { get; set; }
    public int Time { get; set; }
}

var list = new SortedList<MyTimeData>();

// Add items to sorted list

// Reverse to make it descending
var listDescending = list.Reverse();  

foreach (var item in listDescending)
{
    // Do something with item
}
于 2013-08-13T18:44:39.553 回答
0
yourList.OrderByDescending(x => x.Time);
于 2013-08-13T18:40:20.017 回答