lambda c# 排序



关于排序:
做排行榜遇到了排序的问题,查看了官网的例子,点击打开链接自己也顺便写了个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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值