回答編集履歴
2
追記
test
CHANGED
@@ -1,8 +1,132 @@
|
|
1
|
+
完全な自己満足ですが、こういう方針にしてほしいという回答にあらためます。
|
2
|
+
|
3
|
+
* なんでもList<string>でやっちゃだめ。解決したい課題に適したクラスを設計する。じゃないとGUIにもまともにバインディングできない
|
4
|
+
|
5
|
+
* データの保存とかどうすんの?CSVでやったけどまあデータストアがなんでも、そっから読むやつとそこに書くやつがいればいい。
|
6
|
+
|
7
|
+
* クラスがあれば、`OrderBy(datum => datum.Score)`みたいにスコアでソートするんだなーって分かる
|
8
|
+
|
9
|
+
|
10
|
+
|
1
11
|
```csharp
|
2
12
|
|
13
|
+
using System;
|
14
|
+
|
15
|
+
using System.Collections.Generic;
|
16
|
+
|
17
|
+
using System.Linq;
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
namespace ConsoleApp1
|
22
|
+
|
23
|
+
{
|
24
|
+
|
25
|
+
public class Program
|
26
|
+
|
27
|
+
{
|
28
|
+
|
29
|
+
public static void Main(string[] args)
|
30
|
+
|
31
|
+
{
|
32
|
+
|
33
|
+
var path = "??file.csv";
|
34
|
+
|
35
|
+
var usami = DrillLoader(path);
|
36
|
+
|
3
|
-
var sorted =
|
37
|
+
var sorted = usami.OrderBy(d => d.Score).ThenBy(d => d.No);
|
38
|
+
|
39
|
+
foreach(var drill in sorted)
|
40
|
+
|
41
|
+
{
|
42
|
+
|
43
|
+
Console.WriteLine($"番号:{drill.No}、問題:{drill.Q}、答え:{drill.A}、スコア:{drill.Score}");
|
44
|
+
|
45
|
+
}
|
46
|
+
|
47
|
+
}
|
48
|
+
|
49
|
+
public static IEnumerable<Drill> DrillLoader(string path) => System.IO.File.ReadAllLines(path).Select(l => new Drill(l));
|
50
|
+
|
51
|
+
public static void DrillWriter(string path, IEnumerable<Drill> dat) => System.IO.File.WriteAllLines(path, dat.Select(d => d.ToCSV()));
|
52
|
+
|
53
|
+
}
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
public class Drill
|
58
|
+
|
59
|
+
{
|
60
|
+
|
61
|
+
public int No { get; set; } = 0;
|
62
|
+
|
63
|
+
public string Q { get; set; } = string.Empty;
|
64
|
+
|
65
|
+
public string A { get; set; } = string.Empty;
|
66
|
+
|
67
|
+
public int Score { get; set; } = 0;
|
68
|
+
|
69
|
+
private Drill() {}
|
70
|
+
|
71
|
+
public Drill(string csvRow)
|
72
|
+
|
73
|
+
{
|
74
|
+
|
75
|
+
var sep = ',';
|
76
|
+
|
77
|
+
var row = csvRow.Split(sep);
|
78
|
+
|
79
|
+
switch(row.Length)
|
80
|
+
|
81
|
+
{
|
82
|
+
|
83
|
+
case 3:
|
84
|
+
|
85
|
+
No = int.Parse(row[0]); Q = row[1]; A = row[2]; Score = 0;
|
86
|
+
|
87
|
+
return;
|
88
|
+
|
89
|
+
case 4:
|
90
|
+
|
91
|
+
No = int.Parse(row[0]); Q = row[1]; A = row[2]; Score = int.Parse(row[3]);
|
92
|
+
|
93
|
+
return;
|
94
|
+
|
95
|
+
default: throw new ArgumentException();
|
96
|
+
|
97
|
+
}
|
98
|
+
|
99
|
+
//csvにするかどうかは知らんけども"1,信号が青のとき交差点に進入してよい,×,2"みたいなのが何行もあるのを用意する
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
public Drill(int no, string q, string a, int score)
|
104
|
+
|
105
|
+
{
|
106
|
+
|
107
|
+
No = no; Q = q; A = a; Score = score;
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
public Drill(int no, string q, string a)
|
112
|
+
|
113
|
+
{
|
114
|
+
|
115
|
+
No = no; Q = q; A = a; Score = 0;
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
public string ToCSV() => $"{No},{Q},{A},{Score}";
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
}
|
4
124
|
|
5
125
|
```
|
126
|
+
|
127
|
+
---
|
128
|
+
|
129
|
+
質問追記があったので改訂
|
6
130
|
|
7
131
|
|
8
132
|
|
@@ -24,18 +148,36 @@
|
|
24
148
|
|
25
149
|
usami.Add(3,a);
|
26
150
|
|
27
|
-
var sorted = usami.Select(d => d.Value).OrderBy(x => x[1])
|
151
|
+
var sorted = usami.Select(d => new { no = d.Key, val = d.Value}).OrderBy(x => x.val[1]);
|
28
152
|
|
29
153
|
foreach(var item in sorted)
|
30
154
|
|
31
155
|
{
|
32
156
|
|
33
|
-
Console.WriteLine(item.
|
157
|
+
Console.WriteLine($"{item.no}番:{string.Join(",",item.val)}");
|
34
158
|
|
35
159
|
}
|
36
160
|
|
37
161
|
```
|
38
162
|
|
39
|
-
失礼ながら、Dictionaryにする意味およびもともとの1,2,3が何の役目を果たしているかわからないので、番号は振り直しました。
|
163
|
+
~~失礼ながら、Dictionaryにする意味およびもともとの1,2,3が何の役目を果たしているかわからないので、番号は振り直しました。~~
|
40
164
|
|
41
|
-
番号を維持することもできますが、この路線でいくより、本当にやりたかったことに立ち返って検討したほうがいいように思います。DictionaryはKeyから直接アクセスできるところにメリットがあるはずで、Dictionaryを並べ替えなければならない時点であまり良い攻め方ではないと思います。
|
165
|
+
~~番号を維持することもできますが、この路線でいくより、本当にやりたかったことに立ち返って検討したほうがいいように思います。DictionaryはKeyから直接アクセスできるところにメリットがあるはずで、Dictionaryを並べ替えなければならない時点であまり良い攻め方ではないと思います。~~
|
166
|
+
|
167
|
+
Dictionaryは順序を持たないので、一旦匿名型にDictのキーである(おそらく)問題番号を保存して、IEnumerable<a'{no<int>,val<List<string>>}>にしてからソートして、ソートしたものを出力しています。
|
168
|
+
|
169
|
+
繰り返しになりますが、Dictであるusamiというデータセットは型として順序を持ちません。これをソートしようという試みは問題解決として不適切な方針です
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
---
|
174
|
+
|
175
|
+
obsolete
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
```csharp
|
180
|
+
|
181
|
+
var sorted = data.OrderBy(x=>x[1]);
|
182
|
+
|
183
|
+
```
|
1
re
test
CHANGED
@@ -3,3 +3,39 @@
|
|
3
3
|
var sorted = data.OrderBy(x=>x[1]);
|
4
4
|
|
5
5
|
```
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
なんとなくデータ型に混乱があるように思えますが、こういう感じかなというのを。
|
10
|
+
|
11
|
+
```csharp
|
12
|
+
|
13
|
+
var a = new List<string> { "バナナ", "C", "120" };
|
14
|
+
|
15
|
+
var b = new List<string> { "りんご", "A", "100" };
|
16
|
+
|
17
|
+
var c = new List<string> { "みかん", "B", "40" };
|
18
|
+
|
19
|
+
var usami= new Dictionary<int, List<string>>();
|
20
|
+
|
21
|
+
usami.Add(1, c);
|
22
|
+
|
23
|
+
usami.Add(2,b);
|
24
|
+
|
25
|
+
usami.Add(3,a);
|
26
|
+
|
27
|
+
var sorted = usami.Select(d => d.Value).OrderBy(x => x[1]).Select((x,i)=>new { idx=i, lst=x}).ToDictionary(y => y.idx + 1, y=>y.lst);
|
28
|
+
|
29
|
+
foreach(var item in sorted)
|
30
|
+
|
31
|
+
{
|
32
|
+
|
33
|
+
Console.WriteLine(item.Key+" "+string.Join(",",item.Value));
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
```
|
38
|
+
|
39
|
+
失礼ながら、Dictionaryにする意味およびもともとの1,2,3が何の役目を果たしているかわからないので、番号は振り直しました。
|
40
|
+
|
41
|
+
番号を維持することもできますが、この路線でいくより、本当にやりたかったことに立ち返って検討したほうがいいように思います。DictionaryはKeyから直接アクセスできるところにメリットがあるはずで、Dictionaryを並べ替えなければならない時点であまり良い攻め方ではないと思います。
|