关于排序:
做排行榜遇到了排序的问题,查看了官网的例子,点击打开链接自己也顺便写了个test例子
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
public class Program
{
public static List<KeyValuePair<int, string>> list_type;
static Dictionary<int, string> type_value = new Dictionary<int, string>();
public static void Main(string[] args)
{
type_value = addKey(type_value);
list_type = new List<KeyValuePair<int, string>>(type_value);
Console.WriteLine("before");
foreach (var item in list_type)
{
Console.WriteLine("" + item.Key + " " + item.Value);
}
list_type.Sort((sort1,sort2)
=>
{
if (sort1.Key > sort2.Key)
{
return 1;
}
else if (sort1.Key == sort2.Key)
{
return 0;
}
else
return -1;
});
Console.WriteLine("after");
foreach (var item in list_type)
{
Console.WriteLine(""+item.Key+" "+item.Value);
}
list_type.Reverse();
Console.WriteLine("Reverse");
foreach (var item in list_type)
{
Console.WriteLine("" + item.Key + " " + item.Value);
}
Console.ReadLine();
}
public static Dictionary<int,string> addKey(Dictionary<int,string> dic)
{
dic.Add(5,"int");
dic.Add(4,"float");
dic.Add(6, "double");
dic.Add(1, "short");
dic.Add(7, "long");
return dic;
}
}
}
before
5 int
4 float
6 double
1 short
7 long
after
1 short
4 float
5 int
6 double
7 long
Reverse
7 long
6 double
5 int
4 float
1 short