-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
175 lines (136 loc) · 6.15 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.ComponentModel;
using System.Linq;
using linqshared;
namespace linq_ordering
{
class Program: ProgramBase
{
static void Main(string[] args)
{
Linq28();
// Linq29();
// Linq30();
// Linq31();
// Linq32();
// Linq33();
// Linq34();
// Linq35();
// Linq36();
// Linq37();
// Linq38();
// Linq39();
}
[Category("Ordering Operators")]
[Description("This sample uses ordering to sort a list of words alphabetically.")]
static void Linq28()
{
var words = new [] { "cherry", "apple", "blueberry" };
var sortedWords = words.OrderBy(w => w);
Console.WriteLine("The sorted list of words:");
sortedWords.ForEach(Console.WriteLine);
}
[Category("Ordering Operators")]
[Description("This sample uses ordering to sort a list of words by length.")]
static void Linq29()
{
var words = new [] { "cherry", "apple", "blueberry" };
var sortedWords = words.OrderBy(w => w.Length);
Console.WriteLine("The sorted list of words (by length):");
sortedWords.ForEach(Console.WriteLine);
}
[Category("Ordering Operators")]
[Description("This sample uses ordering to sort a list of products by name.")]
static void Linq30()
{
var products = GetProductList();
var sortedProducts = products.OrderBy(p => p.ProductName);
ObjectDumper.Write(sortedProducts);
}
[Category("Ordering Operators")]
[Description("This sample uses case-insensitive ordering to sort the words in an array.")]
static void Linq31()
{
var words = new [] { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var sortedWords = words.OrderBy(a => a, StringComparer.CurrentCultureIgnoreCase);
ObjectDumper.Write(sortedWords);
}
[Category("Ordering Operators")]
[Description("This sample uses reverse ordering to sort a list of doubles from highest to lowest.")]
static void Linq32()
{
var doubles = new[]{ 1.7, 2.3, 1.9, 4.1, 2.9 };
var sortedDoubles = doubles.OrderByDescending(d => d);
Console.WriteLine("The doubles from highest to lowest:");
sortedDoubles.ForEach(Console.WriteLine);
}
[Category("Ordering Operators")]
[Description("This sample uses reverse ordering to sort a list of products by units in stock from highest to lowest.")]
static void Linq33()
{
var products = GetProductList();
var sortedProducts = products.OrderByDescending(p => p.UnitsInStock);
ObjectDumper.Write(sortedProducts);
}
[Category("Ordering Operators")]
[Description("This sample uses reverse case-insensitive ordering to sort the words in an array.")]
static void Linq34()
{
var words = new [] { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var sortedWords = words.OrderByDescending(a => a, StringComparer.CurrentCultureIgnoreCase);
ObjectDumper.Write(sortedWords);
}
[Category("Ordering Operators")]
[Description("This sample uses nested ordering, first by length of their name, and then alphabetically by the name itself.")]
static void Linq35()
{
var digits = new [] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var sortedDigits = digits
.OrderBy(d => d.Length)
.ThenBy(d => d);
Console.WriteLine("Sorted digits:");
sortedDigits.ForEach(Console.WriteLine);
}
[Category("Ordering Operators")]
[Description("This sample uses case-insensitive nested ordering, with a custom comparer to sort first by word length and then by a case-insensitive sort of the words in an array.")]
static void Linq36()
{
var words = new [] { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var sortedWords = words
.OrderBy(a => a.Length)
.ThenBy(a => a, StringComparer.CurrentCultureIgnoreCase);
ObjectDumper.Write(sortedWords);
}
[Category("Ordering Operators")]
[Description("This sample uses nested ordering to sort a list of products, first by category, and then by unit price, from highest to lowest.")]
static void Linq37()
{
var products = GetProductList();
var sortedProducts = products
.OrderBy(p => p.Category)
.ThenByDescending(p => p.UnitPrice);
ObjectDumper.Write(sortedProducts);
}
[Category("Ordering Operators")]
[Description("This sample uses uses case-insensitive reverse nested ordering to sort first by word length and then by a case-insensitive descending sort of the words in an array.")]
static void Linq38()
{
var words = new [] { "aPPLE", "AbAcUs", "bRaNcH", "BlUeBeRrY", "ClOvEr", "cHeRry" };
var sortedWords = words
.OrderBy(a => a.Length)
.ThenByDescending(a => a, StringComparer.CurrentCultureIgnoreCase);
ObjectDumper.Write(sortedWords);
}
[Category("Ordering Operators")]
[Description("This sample uses reverse ordering 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.")]
static void Linq39()
{
var digits = new [] { "zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine" };
var reversedIDigits = digits
.Where(d => d[1] == 'i')
.Reverse();
Console.WriteLine("A backwards list of the digits with a second character of 'i':");
reversedIDigits.ForEach(Console.WriteLine);
}
}
}