Linq101-Ordering

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4
  5 namespace Linq101
  6 {
  7     class Ordering
  8     {
  9         /// <summary>
 10         /// This sample uses orderby to sort a list of words alphabetically.
 11         /// </summary>
 12         public void Linq28()
 13         {
 14             string[] words = { "cherry", "apple", "blueberry" };
 15
 16             var sortedWords = from w in words
 17                               orderby w
 18                               select w;
 19
 20             Console.WriteLine("The sorted list of words:");
 21             foreach (var word in sortedWords)
 22             {
 23                 Console.WriteLine(word);
 24             }
 25         }
 26
 27         /// <summary>
 28         /// This sample uses orderby to sort a list of words by length.
 29         /// </summary>
 30         public void Linq29()
 31         {
 32             string[] words = { "cherry", "apple", "blueberry" };
 33
 34             var sortedWords = from w in words
 35                               orderby w.Length
 36                               select w;
 37
 38             Console.WriteLine("The sorted list of words( by length ):");
 39             foreach (var word in sortedWords)
 40             {
 41                 Console.WriteLine(word);
 42             }
 43         }
 44
 45         /// <summary>
 46         /// This sample uses orderby to sort a list of products by name.
 47         /// </summary>
 48         public void Linq30()
 49         {
 50             var products = Data.GetProductList();
 51
 52             var query = from p in products
 53                         orderby p.ProductName
 54                         select p;
 55
 56             ObjectDumper.Write(query);
 57         }
 58
 59         /// <summary>
 60         /// This sample uses an OrderBy clause with a custom comparer to do a case-insensitive sort of the words in an array.
 61         /// </summary>
 62         public void Linq31()
 63         {
 64             string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
 65
 66             var query = words.OrderBy(w => w, new CaseInsensitiveComparer());
 67
 68             ObjectDumper.Write(query);
 69         }
 70
 71         private class CaseInsensitiveComparer : IComparer<string>
 72         {
 73             public int Compare(string x, string y)
 74             {
 75                 return string.Compare(x, y, StringComparison.InvariantCultureIgnoreCase);
 76             }
 77         }
 78
 79         /// <summary>
 80         /// This sample uses orderby and descending to sort a list of doubles from highest to lowest.
 81         /// </summary>
 82         public void Linq32()
 83         {
 84             double[] doubles = { 1.7, 2.3, 1.9, 4.1, 2.9 };
 85
 86             var sortedDoubles = from d in doubles
 87                                 orderby d descending
 88                                 select d;
 89
 90             ObjectDumper.Write(sortedDoubles);
 91         }
 92
 93         /// <summary>
 94         /// This sample uses orderby to sort a list of products by units in stock from highest to lowest.
 95         /// </summary>
 96         public void Linq33()
 97         {
 98             var products = Data.GetProductList();
 99
100             var query = from p in products
101                         orderby p.UnitsInStock descending
102                         select p;
103
104             ObjectDumper.Write(query);
105         }
106
107         /// <summary>
108         /// This sample uses an OrderBy clause with a custom comparer to do a case-insensitive descending sort of the words in an array.
109         /// </summary>
110         public void Linq34()
111         {
112             string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
113
114             var query = words.OrderByDescending(w => w, new CaseInsensitiveComparer());
115
116             ObjectDumper.Write(query);
117         }
118
119         /// <summary>
120         /// This sample uses a compound orderby to sort a list of digits, first by length of their name, and then alphabetically by the name itself.
121         /// </summary>
122         public void Linq35()
123         {
124             string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
125
126             var sortedDigits = from d in digits
127                                orderby d.Length, d
128                                select d;
129
130             Console.WriteLine("Sorted digits:");
131             foreach (var d in sortedDigits)
132             {
133                 Console.WriteLine(d);
134             }
135         }
136
137         /// <summary>
138         /// This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive sort of the words in an array.
139         /// </summary>
140         public void Linq36()
141         {
142             string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
143
144             var sortedWords = words
145                 .OrderBy(w => w.Length)
146                 .ThenBy(w => w, new CaseInsensitiveComparer());
147
148             ObjectDumper.Write(sortedWords);
149         }
150
151         /// <summary>
152         /// This sample uses a compound orderby to sort a list of products, first by category, and then by unit price, from highest to lowest.
153         /// </summary>
154         public void Linq37()
155         {
156             var products = Data.GetProductList();
157
158             var query = from p in products
159                         orderby p.Category, p.UnitPrice descending
160                         select p;
161
162             ObjectDumper.Write(query);
163         }
164
165         /// <summary>
166         /// This sample uses an OrderBy and a ThenBy clause with a custom comparer to sort first by word length and then by a case-insensitive descending sort of the words in an array.
167         /// </summary>
168         public void Linq38()
169         {
170             string[] words = { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
171
172             var sortedWords = words.OrderBy(w => w.Length).ThenByDescending(w => w, new CaseInsensitiveComparer());
173
174             ObjectDumper.Write(sortedWords);
175         }
176
177         /// <summary>
178         /// This sample uses Reverse to create a list of all digits in the array whose second letter is ‘i‘ that is reversed from the order in the original array.
179         /// </summary>
180         public void Linq39()
181         {
182             string[] digits = { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
183
184             var sortedDigits = (from d in digits
185                                 where d[1] == ‘i‘
186                                 select d)
187                                 .Reverse();
188
189             ObjectDumper.Write(sortedDigits);
190         }
191     }
192 }
时间: 2024-10-10 23:54:21

Linq101-Ordering的相关文章

Guava源码学习(二)Ordering

基于版本:Guava 22.0 Wiki:Ordering 0. Ordering简介 Guava的Ordering提供了链式风格的比较器的实现,我们可以用Ordering轻松构建复杂的比较器. 1. 类图 这张类图不完全,实际上Ordering有十几个子类,这些子类共同提供了复杂的功能. 2. 设计思路 Ordering是继承于java.util.Comparator接口的抽象类,它的十几个子类都实现了compare与equals方法,这些子类可以实现基本的排序功能. 通过链式调用,可以将这些

[SOJ] Ordering Tasks

1940. Ordering Tasks Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been executed. Input

std::sort要求strict weak ordering

strict weak ordering简单地说就是小于语义,非小于等于语义,也就是说对于相等的或者异常的元素比较应当返回false 后果很严重,在google搜一下violating strict weak ordering make std::sort crash能看到很多种后果, 经测试,当待排序元素大于16个时使用std::sort,若传入的callable违反strict weak ordering,则可能死循环也可能越界访问. 待排序元素小于等于16个不会有问题是因为std::sor

UVA - 10305 - Ordering Tasks (拓扑排序!)

UVA - 10305 Ordering Tasks Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Submit Status Description Problem F Ordering Tasks Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n task

Guava学习笔记(4):Ordering犀利的比较器

转自:http://www.cnblogs.com/peida/p/Guava_Ordering.html Ordering是Guava类库提供的一个犀利强大的比较器工具,Guava的Ordering和JDK Comparator相比功能更强.它非常容易扩展,可以轻松构造复杂的comparator,然后用在容器的比较.排序等操作中. 本质上来说,Ordering 实例无非就是一个特殊的Comparator 实例.Ordering只是需要依赖于一个比较器(例如,Collections.max)的方

Distributed Cache Coherence at Scalable Requestor Filter Pipes that Accumulate Invalidation Acknowledgements from other Requestor Filter Pipes Using Ordering Messages from Central Snoop Tag

A multi-processor, multi-cache system has filter pipes that store entries for request messages sent to a central coherency controller. The central coherency controller orders requests from filter pipes using coherency rules but does not track complet

Snoop resynchronization mechanism to preserve read ordering

A processor employing a post-cache (LS2) buffer. Loads are stored into the LS2buffer after probing the data cache. The load/store unit snoops the loads in the LS2?buffer against snoop requests received from an external bus. If a snoop invalidate requ

Reliability, Ordering and Congestion Avoidance over UDP

Introduction Hi, I’m Glenn Fiedler and welcome to the fourth article in my series Networking for Game Programmers In the previous article, we added our own concept of virtual connection on top of UDP. Now we’re going to add reliability, ordering and

UVA10305 Ordering Tasks【DFS】【拓扑排序】

Ordering Tasks Input: standard input Output: standard output Time Limit: 1 second Memory Limit: 32 MB John has n tasks to do. Unfortunately, the tasks are not independent and the execution of one task is only possible if other tasks have already been

Method and apparatus for providing total and partial store ordering for a memory in multi-processor system

An improved memory model and implementation is disclosed. The memory model includes a Total Store Ordering (TSO) and Partial Store Ordering (PSO) memory model to provide a partial order for the memory operations which are issued by multiple processor