如何在ObjC中对数组进行排序

时间:2021-12-13 15:58:55

I'm looking to sort an array (mainArray), containing a few arrays (array1,2,3,...)
The Comparator(NSnumber) is located at index8 in each array1,2,3...

我正在寻找一个数组(mainArray),包含一些数组(array1,2,3,...)比较器(NSnumber)位于每个array1,2,3的index8 ...

Which method am i supposed to use ?

我应该使用哪种方法?

Would you have any solution for this ?
Thanks a lot

你有任何解决方案吗?非常感谢

Cheers

干杯

Tim

蒂姆

2 个解决方案

#1


1  

If you sort the NSArray using a comparator, you have full control of every comparison.
So you can use:

如果使用比较器对NSArray进行排序,则可以完全控制每次比较。所以你可以使用:

array=[array sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2)
{
    // obj1 and obj2 are two arrays
    NSNumber* n1=[obj1 objectAtIndex:8];
    NSNumber* n2=[obj2 objectAtIndex: 8];
    <compare the numbers and return NSOrderedAscending, NSOrderedSame or NSOrderedDescending>
}];

#2


1  

You can sort a NSMutableArray.

您可以对NSMutableArray进行排序。

// Create an array of arrays of integers using the new array and number literal syntax 
NSMutableArray* arrayOfArrays = 
    [NSMutableArray arrayWithArray:@[ @[ @0, @1, @2 ], @[ @1, @2, @3 ] ];

// The key on which to sort is located at this index in each array
NSUInteger sortKeyIndex = 2;

// Sort the array of arrays using a stable sort. This means that arrays which considered
// equal in arrayOfArrays will retain their relative positions in the sorted arrayOfArrays
// Use a comparator block to sort arrayOfArrays. 
[arrayOfArrays sortWithOptions:NSSortStable 
               usingComparator:^(id lhs, id rhs) {
    // Cast the two elements of arrayOfArrays being compared to their actual type (NSArray)
    NSArray* lhsArray = lhs;
    NSArray* rhsArray = rhs;

    // Extract the element of each array which is used as a sort key
    // This makes use of the new array index syntax
    NSNumber* lhsNumber = lhsArray[sortKeyIndex];
    NSNumber* rhsNumber = rhs[sortKeyIndex];

    // Unbox the integers from each NSNumber to get the actual values of the sort keys
    NSInteger lhsSortKey = [lhs integerValue];
    NSInteger rhsSortKey = [rhs integerValue];

    // Compare the two sort keys
    if (lhsSortKey < rhsSortKey) {
        return NSOrderedAscending;
    } else if (lhsSortKey > rhsSortKey) {
        return NSOrderedDescending;
    } else {
        return NSOrderedSame;
    }
}];

#1


1  

If you sort the NSArray using a comparator, you have full control of every comparison.
So you can use:

如果使用比较器对NSArray进行排序,则可以完全控制每次比较。所以你可以使用:

array=[array sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2)
{
    // obj1 and obj2 are two arrays
    NSNumber* n1=[obj1 objectAtIndex:8];
    NSNumber* n2=[obj2 objectAtIndex: 8];
    <compare the numbers and return NSOrderedAscending, NSOrderedSame or NSOrderedDescending>
}];

#2


1  

You can sort a NSMutableArray.

您可以对NSMutableArray进行排序。

// Create an array of arrays of integers using the new array and number literal syntax 
NSMutableArray* arrayOfArrays = 
    [NSMutableArray arrayWithArray:@[ @[ @0, @1, @2 ], @[ @1, @2, @3 ] ];

// The key on which to sort is located at this index in each array
NSUInteger sortKeyIndex = 2;

// Sort the array of arrays using a stable sort. This means that arrays which considered
// equal in arrayOfArrays will retain their relative positions in the sorted arrayOfArrays
// Use a comparator block to sort arrayOfArrays. 
[arrayOfArrays sortWithOptions:NSSortStable 
               usingComparator:^(id lhs, id rhs) {
    // Cast the two elements of arrayOfArrays being compared to their actual type (NSArray)
    NSArray* lhsArray = lhs;
    NSArray* rhsArray = rhs;

    // Extract the element of each array which is used as a sort key
    // This makes use of the new array index syntax
    NSNumber* lhsNumber = lhsArray[sortKeyIndex];
    NSNumber* rhsNumber = rhs[sortKeyIndex];

    // Unbox the integers from each NSNumber to get the actual values of the sort keys
    NSInteger lhsSortKey = [lhs integerValue];
    NSInteger rhsSortKey = [rhs integerValue];

    // Compare the two sort keys
    if (lhsSortKey < rhsSortKey) {
        return NSOrderedAscending;
    } else if (lhsSortKey > rhsSortKey) {
        return NSOrderedDescending;
    } else {
        return NSOrderedSame;
    }
}];