回答編集履歴
2
Piont nullの場合追加
test
CHANGED
@@ -122,6 +122,8 @@
|
|
122
122
|
|
123
123
|
{
|
124
124
|
|
125
|
+
if (other == null) return 1;
|
126
|
+
|
125
127
|
return (this.X + this.Y) - (other.X + other.Y);
|
126
128
|
|
127
129
|
}
|
1
サンプルを書いてみました
test
CHANGED
@@ -1,3 +1,135 @@
|
|
1
1
|
そんな変なことはせず、並べ替えたい型を作って IComparable か IComparable<T>を実装してください。
|
2
2
|
|
3
3
|
Number と言うプロパティを作ったらその型は整数型かそのNull許容型にしてください。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
サンプルを書いてみました。
|
8
|
+
|
9
|
+
```C#
|
10
|
+
|
11
|
+
namespace ConsoleApp1
|
12
|
+
|
13
|
+
{
|
14
|
+
|
15
|
+
class Program
|
16
|
+
|
17
|
+
{
|
18
|
+
|
19
|
+
static void Main(string[] args)
|
20
|
+
|
21
|
+
{
|
22
|
+
|
23
|
+
var sample = new[]
|
24
|
+
|
25
|
+
{
|
26
|
+
|
27
|
+
new SampleClass
|
28
|
+
|
29
|
+
{
|
30
|
+
|
31
|
+
Number = 10,
|
32
|
+
|
33
|
+
Point = new Point
|
34
|
+
|
35
|
+
{
|
36
|
+
|
37
|
+
X = 1,
|
38
|
+
|
39
|
+
Y = 10
|
40
|
+
|
41
|
+
},
|
42
|
+
|
43
|
+
Color = ConsoleColor.Black
|
44
|
+
|
45
|
+
},
|
46
|
+
|
47
|
+
new SampleClass
|
48
|
+
|
49
|
+
{
|
50
|
+
|
51
|
+
Point = new Point
|
52
|
+
|
53
|
+
{
|
54
|
+
|
55
|
+
X = 5,
|
56
|
+
|
57
|
+
Y = 5
|
58
|
+
|
59
|
+
}
|
60
|
+
|
61
|
+
},
|
62
|
+
|
63
|
+
new SampleClass
|
64
|
+
|
65
|
+
{
|
66
|
+
|
67
|
+
Number = 5,
|
68
|
+
|
69
|
+
Color = ConsoleColor.Cyan
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
};
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
var sortedSample = sample
|
78
|
+
|
79
|
+
.OrderBy(s => s.Number ?? int.MaxValue)
|
80
|
+
|
81
|
+
.ThenBy(s => s.Point)
|
82
|
+
|
83
|
+
.ThenBy(s => s.Color == null ? int.MaxValue : (int)s.Color);
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
}
|
88
|
+
|
89
|
+
}
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
class SampleClass
|
94
|
+
|
95
|
+
{
|
96
|
+
|
97
|
+
public int ? Number { get; set; }
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
public Point Point { get; set; }
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
public ConsoleColor? Color { get; set; }
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
public class Point : IComparable<Point>
|
112
|
+
|
113
|
+
{
|
114
|
+
|
115
|
+
public int X { get; set; }
|
116
|
+
|
117
|
+
public int Y { get; set; }
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
public int CompareTo(Point other)
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
return (this.X + this.Y) - (other.X + other.Y);
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
}
|
134
|
+
|
135
|
+
```
|