I'm able to do this:
我可以这样做:
File images = new File(path);
File[] imageList = images.listFiles(new FilenameFilter(){
public boolean accept(File dir, String name)
{
return name.endsWith(".jpg");
}
});
I copied from an answer of * !
我从*的答案中复制过来!
Is there a way to listFile ok kind "directory" (folder) and to list by reverse alphabetical order ? ... And by reverse date ?
是否有一种方法可以列出ok类的“目录”(文件夹)和按反向字母顺序列出?…反过来呢?
8 个解决方案
#1
18
final File[] sortedFileName = images.listFiles()
if (sortedFileName != null && sortedFileName.length > 1) {
Arrays.sort(sortedFileName, new Comparator<File>() {
@Override
public int compare(File object1, File object2) {
return object1.getName().compareTo(object2.getName());
}
});
}
Use Array.sort()
to compare the name of the files.
使用Array.sort()比较文件的名称。
Edit: Use this code to sort by date
编辑:使用此代码按日期排序
final File[] sortedByDate = folder.listFiles();
if (sortedByDate != null && sortedByDate.length > 1) {
Arrays.sort(sortedByDate, new Comparator<File>() {
@Override
public int compare(File object1, File object2) {
return (int) ((object1.lastModified() > object2.lastModified()) ? object1.lastModified(): object2.lastModified());
}
});
}
#2
2
Pointers :
指针:
File.isDirectory()
File.lastModified()
#3
1
The right way to order files by date of last modified in reverse mode is doing:
按最后修改日期以反向方式订购文件的正确方法是:
Arrays.sort(Files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
for this instruction you will need the commons.io library from org.apache that you can download from here
对于这个指令,你将需要公有物。io库从org。可以从这里下载的apache
#4
1
you can use below code. worked for me.
您可以使用下面的代码。为我工作。
final File[] files= file.listFiles();
if (files != null && files.length > 1) {
Collections.sort(Arrays.asList(files), new Comparator<File>() {
public int compare(File o1, File o2) {
long lastModifiedO1 = o1.lastModified();
long lastModifiedO2 = o2.lastModified();
return (lastModifiedO2 < lastModifiedO1) ? -1 : ((lastModifiedO1 > lastModifiedO2) ? 1 : 0);
}
});
}
#5
1
I was getting an exception
我有个例外
IllegalArgumentException: Comparison method violates its general contract!
例外:比较方法违反了它的一般合同!
so I used this and it worked ok:
我用了这个,效果不错
Arrays.sort(filesList, new Comparator<File>() {
@Override
public int compare(File a, File b) {
if(a.lastModified() < b.lastModified() )
return 1;
if(a.lastModified() > b.lastModified() )
return -1;
return 0;
}});
#6
0
If you want to sort date wise then choose below code, and if you want name then already answer is above :)
如果你想按日期排序,请选择下面的代码,如果你想要名字,那么答案已经在上面了:)
File f = new File("/home/myfiles");
File [] files = f.listFiles();
Arrays.sort( files, new Comparator(){
public int compare(Object o1, Object o2) {
if (((File)o1).lastModified() > ((File)o2).lastModified()) {
return -1;
} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
return +1;
} else {
return 0;
}
}
});
#7
0
I solved this problem by using this
我用这个解决了这个问题
final File[] sortedByDate = folder.listFiles();
if (sortedByDate != null && sortedByDate.length > 1) {
Arrays.sort(sortedByDate, new Comparator<File>() {
@Override
public int compare(File object1, File object2) {
return (int) ((object1.lastModified() > object2.lastModified()) ? 0: 1);
}
});
}
I return 0 and 1 insted of lastModified()
value that solved my problem.
我返回0和1 insted的lastModified()值,它解决了我的问题。
#8
0
For sort based on file name,Add
对于基于文件名的排序,请添加。
object1.getName().toLowerCase(Locale.getDefault())
instead of
而不是
object1.getName()
to avoid sort issues causing by Locale changes and upper/low case filenames
避免由于语言环境更改和上/小写文件名引起的排序问题。
final File[] sortedFileName = images.listFiles();
if (sortedFileName != null && sortedFileName.length > 1) {
Arrays.sort(sortedFileName, new Comparator<File>() {
@Override
public int compare(File object1, File object2) {
return object1.getName().toLowerCase(Locale.getDefault()).compareTo(object2.getName().toLowerCase(Locale.getDefault()));
}
});
}
#1
18
final File[] sortedFileName = images.listFiles()
if (sortedFileName != null && sortedFileName.length > 1) {
Arrays.sort(sortedFileName, new Comparator<File>() {
@Override
public int compare(File object1, File object2) {
return object1.getName().compareTo(object2.getName());
}
});
}
Use Array.sort()
to compare the name of the files.
使用Array.sort()比较文件的名称。
Edit: Use this code to sort by date
编辑:使用此代码按日期排序
final File[] sortedByDate = folder.listFiles();
if (sortedByDate != null && sortedByDate.length > 1) {
Arrays.sort(sortedByDate, new Comparator<File>() {
@Override
public int compare(File object1, File object2) {
return (int) ((object1.lastModified() > object2.lastModified()) ? object1.lastModified(): object2.lastModified());
}
});
}
#2
2
Pointers :
指针:
File.isDirectory()
File.lastModified()
#3
1
The right way to order files by date of last modified in reverse mode is doing:
按最后修改日期以反向方式订购文件的正确方法是:
Arrays.sort(Files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
for this instruction you will need the commons.io library from org.apache that you can download from here
对于这个指令,你将需要公有物。io库从org。可以从这里下载的apache
#4
1
you can use below code. worked for me.
您可以使用下面的代码。为我工作。
final File[] files= file.listFiles();
if (files != null && files.length > 1) {
Collections.sort(Arrays.asList(files), new Comparator<File>() {
public int compare(File o1, File o2) {
long lastModifiedO1 = o1.lastModified();
long lastModifiedO2 = o2.lastModified();
return (lastModifiedO2 < lastModifiedO1) ? -1 : ((lastModifiedO1 > lastModifiedO2) ? 1 : 0);
}
});
}
#5
1
I was getting an exception
我有个例外
IllegalArgumentException: Comparison method violates its general contract!
例外:比较方法违反了它的一般合同!
so I used this and it worked ok:
我用了这个,效果不错
Arrays.sort(filesList, new Comparator<File>() {
@Override
public int compare(File a, File b) {
if(a.lastModified() < b.lastModified() )
return 1;
if(a.lastModified() > b.lastModified() )
return -1;
return 0;
}});
#6
0
If you want to sort date wise then choose below code, and if you want name then already answer is above :)
如果你想按日期排序,请选择下面的代码,如果你想要名字,那么答案已经在上面了:)
File f = new File("/home/myfiles");
File [] files = f.listFiles();
Arrays.sort( files, new Comparator(){
public int compare(Object o1, Object o2) {
if (((File)o1).lastModified() > ((File)o2).lastModified()) {
return -1;
} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
return +1;
} else {
return 0;
}
}
});
#7
0
I solved this problem by using this
我用这个解决了这个问题
final File[] sortedByDate = folder.listFiles();
if (sortedByDate != null && sortedByDate.length > 1) {
Arrays.sort(sortedByDate, new Comparator<File>() {
@Override
public int compare(File object1, File object2) {
return (int) ((object1.lastModified() > object2.lastModified()) ? 0: 1);
}
});
}
I return 0 and 1 insted of lastModified()
value that solved my problem.
我返回0和1 insted的lastModified()值,它解决了我的问题。
#8
0
For sort based on file name,Add
对于基于文件名的排序,请添加。
object1.getName().toLowerCase(Locale.getDefault())
instead of
而不是
object1.getName()
to avoid sort issues causing by Locale changes and upper/low case filenames
避免由于语言环境更改和上/小写文件名引起的排序问题。
final File[] sortedFileName = images.listFiles();
if (sortedFileName != null && sortedFileName.length > 1) {
Arrays.sort(sortedFileName, new Comparator<File>() {
@Override
public int compare(File object1, File object2) {
return object1.getName().toLowerCase(Locale.getDefault()).compareTo(object2.getName().toLowerCase(Locale.getDefault()));
}
});
}