回答編集履歴
1
真面目に回答します。
test
CHANGED
@@ -19,3 +19,237 @@
|
|
19
19
|
→第一ソートがサイズで、第二ソートが更新日ということですか?
|
20
20
|
|
21
21
|
全部同一の場合はどちらが優先して表示されても良いのですか?
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
-----追記
|
26
|
+
|
27
|
+
**何を期待して質問したのかわからないのであまり考えていません。**
|
28
|
+
|
29
|
+
**これをそのままコピペして何かの課題なり宿題なりに提出して動かないと言われても責任は負いません。**
|
30
|
+
|
31
|
+
質問事項にイマイチ的を得ない回答を頂いたので、適当に補完しました。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
```Java
|
36
|
+
|
37
|
+
import java.io.File;
|
38
|
+
|
39
|
+
import java.text.SimpleDateFormat;
|
40
|
+
|
41
|
+
import java.util.ArrayList;
|
42
|
+
|
43
|
+
import java.util.Collections;
|
44
|
+
|
45
|
+
import java.util.Comparator;
|
46
|
+
|
47
|
+
import java.util.Date;
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
public class Test {
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
public static void main(String[] args) {
|
56
|
+
|
57
|
+
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
|
58
|
+
|
59
|
+
String path = "C:\\hoge";
|
60
|
+
|
61
|
+
File dir = new File(path);
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
if (!dir.exists()) {
|
66
|
+
|
67
|
+
System.out.println("存在しません");
|
68
|
+
|
69
|
+
return;
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
File[] files = dir.listFiles();
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
ArrayList<FileData> list = new ArrayList<FileData>();
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
for (int i = 0; i < files.length; i++) {
|
82
|
+
|
83
|
+
File file = files[i];
|
84
|
+
|
85
|
+
String fileName = file.getName();
|
86
|
+
|
87
|
+
long size = file.length();
|
88
|
+
|
89
|
+
FileData fd = new FileData();
|
90
|
+
|
91
|
+
fd.setId(i);
|
92
|
+
|
93
|
+
fd.setFileName(fileName);
|
94
|
+
|
95
|
+
fd.setSize(size);
|
96
|
+
|
97
|
+
fd.setLastUpdate(file.lastModified());
|
98
|
+
|
99
|
+
list.add(fd);
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
Collections.sort(list, new FileCompSize());
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
for (FileData fd : list) {
|
110
|
+
|
111
|
+
System.out.println(fd.getId() + " " + fd.getFileName() + " size:" + fd.getSize() );
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
Collections.sort(list, new FileCompLastUpdate());
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
for (FileData fd : list) {
|
122
|
+
|
123
|
+
System.out.println(fd.getId() + " " + fd.getFileName() + " lastUpdate:" + sdf.format(new Date(fd.getLastUpdate())) );
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
static final class FileCompSize implements Comparator<FileData> {
|
134
|
+
|
135
|
+
@Override
|
136
|
+
|
137
|
+
public int compare(FileData f1, FileData f2) {
|
138
|
+
|
139
|
+
int i = Long.compare(f1.getSize(), f2.getSize());
|
140
|
+
|
141
|
+
if (i == 0 ) {
|
142
|
+
|
143
|
+
i = Long.compare(f1.getId(), f2.getId());
|
144
|
+
|
145
|
+
}
|
146
|
+
|
147
|
+
return i;
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
}
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
static final class FileCompLastUpdate implements Comparator<FileData> {
|
158
|
+
|
159
|
+
@Override
|
160
|
+
|
161
|
+
public int compare(FileData f1, FileData f2) {
|
162
|
+
|
163
|
+
int i = Long.compare(f1.getLastUpdate(), f2.getLastUpdate());
|
164
|
+
|
165
|
+
if (i == 0 ) {
|
166
|
+
|
167
|
+
i = Long.compare(f1.getId(), f2.getId());
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
return i;
|
172
|
+
|
173
|
+
}
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
}
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
static final class FileData {
|
182
|
+
|
183
|
+
|
184
|
+
|
185
|
+
private int id;
|
186
|
+
|
187
|
+
private String fileName;
|
188
|
+
|
189
|
+
private long size;
|
190
|
+
|
191
|
+
private long lastUpdate;
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
public int getId() {
|
196
|
+
|
197
|
+
return id;
|
198
|
+
|
199
|
+
}
|
200
|
+
|
201
|
+
public void setId(int id) {
|
202
|
+
|
203
|
+
this.id = id;
|
204
|
+
|
205
|
+
}
|
206
|
+
|
207
|
+
public String getFileName() {
|
208
|
+
|
209
|
+
return fileName;
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
public void setFileName(String fileName) {
|
214
|
+
|
215
|
+
this.fileName = fileName;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
public long getSize() {
|
220
|
+
|
221
|
+
return size;
|
222
|
+
|
223
|
+
}
|
224
|
+
|
225
|
+
public void setSize(long size) {
|
226
|
+
|
227
|
+
this.size = size;
|
228
|
+
|
229
|
+
}
|
230
|
+
|
231
|
+
public long getLastUpdate() {
|
232
|
+
|
233
|
+
return lastUpdate;
|
234
|
+
|
235
|
+
}
|
236
|
+
|
237
|
+
public void setLastUpdate(long lastUpdate) {
|
238
|
+
|
239
|
+
this.lastUpdate = lastUpdate;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
|
246
|
+
|
247
|
+
}
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
```
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
|