订购NSURL数组 [英] Order a NSURL array

查看:92
本文介绍了订购NSURL数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将许多图像路径加载到NSURL中. 图像位于从1.PNG,2.PNG,3.PNG到1500.PNG的文件夹中.
当我尝试加载它们时:

I'm loading a lot of image paths into a NSURL. The images are in a folder ordered from 1.PNG, 2.PNG, 3.PNG to 1500.PNG.
When I trie to load them:

let imagePath = path + "/images"
        let url = NSURL(fileURLWithPath: imagePath)
        print(url)
        let fileManager = NSFileManager.defaultManager()
        let properties = [NSURLLocalizedLabelKey,
                          NSURLCreationDateKey, NSURLLocalizedTypeDescriptionKey]

        do {
            imageURLs = try fileManager.contentsOfDirectoryAtURL(url, includingPropertiesForKeys: properties, options:NSDirectoryEnumerationOptions.SkipsHiddenFiles)
        } catch let error1 as NSError {
            print(error1.description)
        }

imageURLs数组填充为:

The imageURLs array gets filled with:

imageURLs [0] = ... \ 0.PNG
imageURLs [1] = ... \ 1.PNG
imageURLs [2] = ... \ 100.PNG
imageURLs [3] = ... \ 1000.PNG

imageURLs[0] = ...\0.PNG
imageURLs[1] = ...\1.PNG
imageURLs[2] = ...\100.PNG
imageURLs[3] = ...\1000.PNG

,而不是数字顺序!
有人可以帮我对imageURLs进行排序吗,或者当我在其上加载图像路径时还是在加载它们之后?

and not in the numeric order!
Can someone help to sort the imageURLs or while i load the image paths on it or after they are loaded?

推荐答案

由于要按编号对文件进行排序,因此必须首先解析实现该路径的路径,因此,假设我们有以下NSURL数组对象:

As you want to sort the files by the number you have to parse first the path to achieve it, so let's suppose we have the following array of NSURL objects:

var urls = [NSURL(string: "file:///path/to/user/folder/2.PNG")!, NSURL(string: "file:///path/to/user/folder/100.PNG")!, NSURL(string: "file:///path/to/user/folder/101.PNG")!, NSURL(string: "file:///path/to/user/folder/1.PNG")! ]

我们可以使用)路径中的所有组件.

We can use the pathComponents property to extract an array with all the components in the path for a NSURL (e.g ["/", "path", "to", "user", "folder", "2.PNG"]).

如果看到的话,我们可以按数组中的最后一个元素(即文件名,除去扩展名和点号("."),在本例中为数字)对文件排序.让我们看看如何在以下代码中做到这一点:

If we see we can order the files by the last element in the array that is the filename removing the extension and the dot("."), in this case the number. Let's see how to do it in the following code:

urls.sortInPlace {

   // number of elements in each array
   let c1 = $0.pathComponents!.count - 1
   let c2 = $1.pathComponents!.count - 1

   // the filename of each file
   var v1 = $0.pathComponents![c1].componentsSeparatedByString(".")
   var v2 = $1.pathComponents![c2].componentsSeparatedByString(".")

   return Int(v1[0]) < Int(v2[0])
}

在上面的代码中,我们使用函数sortInPlace来避免创建具有已排序元素的另一个数组,但是如果需要,可以使用sort代替.代码中的另一个重要点是return Int(v1[0]) < Int(v2[0])行,在这一行中,我们必须将字符串中的数字转换为实数,因为如果我们将两个字符串"2""100"比较,则第二个字符串要少大于因为按字典顺序比较了字符串.

In the above code we use the function sortInPlace to avoid create another array with the elements sorted, but can you use sort instead if you want. The another important point in the code is the line return Int(v1[0]) < Int(v2[0]), in this line we have to convert the number in the string to a real number, because if we compare the two strings "2" and "100" the second one is less than greater than because the string are compared lexicographically.

因此数组urls应该类似于以下内容:

So the the array urls should be like the following one:

[file:///path/to/user/folder/1.PNG, file:///path/to/user/folder/2.PNG, file:///path/to/user/folder/100.PNG, file:///path/to/user/folder/101.PNG]

两个函数pathComponentscomponentsSeparatedByString增加了sortInPlace算法的空间复杂度,如果可以确保文件的路径始终相同,除了文件名应为数字即可相反,此代码:

The two functions pathComponents and componentsSeparatedByString increase the space complexity of the sortInPlace algorithm, if you can asure that the path for the files always will be the same except it's filename that should be a number you can use instead this code:

urls.sortInPlace { $0.absoluteString.compare(
                   $1.absoluteString, options: .NumericSearch) == .OrderedAscending
}

希望这对您有所帮助.

这篇关于订购NSURL数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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