从文档目录中读取文件时的排序 [英] Sorting of files while reading from document directory

查看:98
本文介绍了从文档目录中读取文件时的排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在阅读应用文档目录中的文件列表。

I am currently reading the list of files present in my app document directory.

NSString* documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
NSError* error = nil;
NSMutableArray  *myArray = (NSMutableArray*)[[[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirectory error:&error]retain];

但是在显示我要显示的文件列表之前,所有文件都应该正确排序。

But before showing the list of files i want to show the all files should be sorted properly.

我该怎么做?

推荐答案

你的myArray将是一个NSStrings数组。由于它们都在同一个目录中,你可以这样排序:

Your myArray will be an array of NSStrings. Since they are all in the same directory you can sort it like this:

NSArray * sortedArray =
 [myArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

现在重要的是要注意 contentsOfDirectoryAtPath:documentDirectory错误:& error 返回目录和文件。所以在获取排序数组之前,如果你只想要文件,

Now it is important to note that contentsOfDirectoryAtPath:documentDirectory error:&error returns both directories and files. So before getting the sorted array, if you want just files,

NSMutableArray *tempArray=[[NSMutableArray alloc] init];
for(NSString *str in myArray)
{
if([[NSFileManager defaultManager] fileExistsAtPath:str]
[tempArray addObject:str];
}

现在对这个tempArray而不是myArray进行排序,你就完成了

Now sort this tempArray instead of myArray and you're done

这篇关于从文档目录中读取文件时的排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆