回答編集履歴
3
追記
test
CHANGED
@@ -53,3 +53,95 @@
|
|
53
53
|
res4: Seq[Member] = List(Member(3,180,60), Member(1,170,60), Member(2,170,55))
|
54
54
|
|
55
55
|
```
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
---
|
60
|
+
|
61
|
+
|
62
|
+
|
63
|
+
randomの意味役割を理解できていませんでした。そこを理解できたつもりで、追記。
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
```scala
|
68
|
+
|
69
|
+
...
|
70
|
+
|
71
|
+
val random = Seq(1,2,3)
|
72
|
+
|
73
|
+
val random_2 = Seq(2,1,3)
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
def sss(members: Seq[Member], randomKey: Seq[Int]): Seq[Member] =
|
78
|
+
|
79
|
+
members.sortBy{
|
80
|
+
|
81
|
+
x =>
|
82
|
+
|
83
|
+
randomKey.map{
|
84
|
+
|
85
|
+
_ match {
|
86
|
+
|
87
|
+
case 1 => x.id
|
88
|
+
|
89
|
+
case 2 => 10000 - x.height
|
90
|
+
|
91
|
+
case 3 => x.weight
|
92
|
+
|
93
|
+
}
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
.map{"%04d".format(_)}
|
98
|
+
|
99
|
+
.mkString(" ")
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
val aa = sss(members, random)
|
106
|
+
|
107
|
+
println(s" keys: ${random}")
|
108
|
+
|
109
|
+
println(s"result: ${aa}")
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
val bb = sss(members, random_2)
|
114
|
+
|
115
|
+
println(s" keys: ${random_2}")
|
116
|
+
|
117
|
+
println(s"result: ${bb}")
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
val random_3 = Seq(3,2,1)
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
val cc = sss(members, random_3)
|
126
|
+
|
127
|
+
println(s" keys: ${random_3}")
|
128
|
+
|
129
|
+
println(s"result: ${cc}")
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
// keys: List(1, 2, 3)
|
136
|
+
|
137
|
+
// result: List(Member(1,170,60), Member(2,170,55), Member(3,180,60))
|
138
|
+
|
139
|
+
// keys: List(2, 1, 3)
|
140
|
+
|
141
|
+
// result: List(Member(3,180,60), Member(1,170,60), Member(2,170,55))
|
142
|
+
|
143
|
+
// keys: List(3, 2, 1)
|
144
|
+
|
145
|
+
// result: List(Member(2,170,55), Member(3,180,60), Member(1,170,60))
|
146
|
+
|
147
|
+
```
|
2
追記
test
CHANGED
@@ -23,3 +23,33 @@
|
|
23
23
|
res15: Seq[Member] = List(Member(1,170,60), Member(2,170,55), Member(3,180,60))
|
24
24
|
|
25
25
|
```
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
---
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
むむ?
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
> random_2の場合は、身長でソートし、同じの場合はidでソート、それでも同じ場合は体重でソート
|
38
|
+
|
39
|
+
> // 求めたい結果
|
40
|
+
|
41
|
+
> List(Member(3,180,60), Member(1,170,60), Member(2,170,55))
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
「身長でソートし」ではなく、「身長を降順ソートし」かよ。
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
```scala
|
50
|
+
|
51
|
+
scala> members.sortBy{x => "%04d %04d %04d".format(10000-x.height, x.id, x.weight)}
|
52
|
+
|
53
|
+
res4: Seq[Member] = List(Member(3,180,60), Member(1,170,60), Member(2,170,55))
|
54
|
+
|
55
|
+
```
|
1
typo
test
CHANGED
@@ -10,7 +10,7 @@
|
|
10
10
|
|
11
11
|
// idでソートし、idが同じの場合は身長でソート、それでも同じ場合は、体重でソート
|
12
12
|
|
13
|
-
scala> members.sortBy
|
13
|
+
scala> members.sortBy{x => "%04d %04d %04d".format(x.id, x.height, x.weight)}
|
14
14
|
|
15
15
|
res14: Seq[Member] = List(Member(1,170,60), Member(2,170,55), Member(3,180,60))
|
16
16
|
|
@@ -18,7 +18,7 @@
|
|
18
18
|
|
19
19
|
// 身長でソートし、同じの場合はidでソート、それでも同じ場合は体重でソート
|
20
20
|
|
21
|
-
scala> members.sortBy
|
21
|
+
scala> members.sortBy{x => "%04d %04d %04d".format(x.height, x.id, x.weight)}
|
22
22
|
|
23
23
|
res15: Seq[Member] = List(Member(1,170,60), Member(2,170,55), Member(3,180,60))
|
24
24
|
|