加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C# 如何对字符串数组排序

(2012-11-26 14:04:45)
标签:

clinq

字串排序

分类: Programm

实例:

   String[] ssss = new String[] { "2011/3", "2011/2", "2011/5", "2012/5" };
   //排序方式一:使用扩展方法

   IEnumerable<String> query = ssss.OrderBy(x => x);

 

   //排序方式二:
   var sortedWords = from s in ssss
                     orderby s ascending //或descending
                     select s;

排序前顺序:

2011/3
2011/2
2011/5
2012/4

 

方式一排序后顺序:

2011/2
2011/3
2011/5
2012/4


方式二排序后顺序:

2011/2
2011/3
2011/5
2012/4

 

技术说明:

命名空间: System.Linq
程序集: System.Core(在 System.Core.dll 中)

静态方法:

        //
        // 摘要:
        //     根据键按升序对序列的元素排序。
        //
        // 参数:
        //   source:
        //     一个要排序的值序列。
        //
        //   keySelector:
        //     用于从元素中提取键的函数。
        //
        // 类型参数:
        //   TSource:
        //     source 中的元素的类型。
        //
        //   TKey:
        //     keySelector 返回的键的类型。
        //
        // 返回结果:
        //     一个 System.Linq.IOrderedEnumerable<TElement>,其元素按键排序。
        //
        // 异常:
        //   System.ArgumentNullException:
        //     source 或 keySelector 为 null。
        public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector);

 

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有