如何在NSMutableArray结构下面排序

时间:2021-11-03 19:20:20

i want below array(10 elements) to be sorted ascending order of
"/Users/bkothari/Library....Photo-0.png" to be 0th element
of an array and so on

我希望下面的数组(10个元素)按照“/Users/bkothari/Library....Photo-0.png”的升序排序为数组的第0个元素,依此类推

I have tried to use

我试过用

NSArray *sorted = [profileImageArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    if ([obj1 intValue] < [obj2 intValue]) return NSOrderedAscending;
    else return NSOrderedDescending;
}];

but its not working.

但它不起作用。

profileImageArray (
    "/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-4.png",
    1,
    1,
    1,
    1,
    1,
    1,
    1,
   "/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-1.png",
    "/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-0.png"
)

Final output required:

最终输出要求:

  profileImageArray (
    "/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-0.png",
     "/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-1.png",
    1,
    1,
    "/Users/bkothari/Library/Application Support/iPhone Simulator/7.0/Applications/233D4F5D-CDB0-4D55-8037-5DD674558BB0/Documents/MyFolder/Photo-4.png",
    1,
    1,
    1,
    1,
    1
)

1 个解决方案

#1


7  

Try with sortedArrayUsingComparator using NSNumericSearch option

使用NSNumericSearch选项尝试使用sortedArrayUsingComparator

try this:

profileImageArray = [profileImageArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];

#1


7  

Try with sortedArrayUsingComparator using NSNumericSearch option

使用NSNumericSearch选项尝试使用sortedArrayUsingComparator

try this:

profileImageArray = [profileImageArray sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [(NSString *)obj1 compare:(NSString *)obj2 options:NSNumericSearch];
}];