views:

548

answers:

2

Hello all, I have an array for instance,

    Array {
    3.0 at Index 0
    2.0 at Index 1
    3.5 at Index 2
    1.0 at Index 4
}

I would like to get sort it in ascending order without losing the index in the first place, like this,

    Array {
    1.0 at Index 4
    2.0 at Index 1
    3.0 at Index 0
    3.5 at Index 2
}

When I sort the array using this,

NSArray *sortedArray = [hArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
[knnRecog sortUsingDescriptors:[NSArray arrayWithObject:sortAsc]];

I lose the index. Does anyone know a way to preserve the index after sorting the array? Thanks

+1  A: 

By definition, you can't sort an array and keep the initial index because sorting is nothing but swapping the indexes around to produce the new sort order. A sort always alters the indexes.

It sounds like what you actually want to do is convert the index of the initial array into data and then store that in realtion with the initial array's data.

If so, then you need an array of dictionaries. The dictionary should related the a single index of the original dictionary with the value at that index:

NSDictionary *anElement=[NSDictionary dictionaryWithObject:[intialArray objectAtIndex:i] forKey:[NSNumber numberWithInt:i]];

Then add each dictionary to an array to store them. You can then sort that array as you wish (using a predicate for the dictionary) without loosing the relationship in the original array.

TechZen
+1 array of dictionaries is the right way to achieve this. Takes a few minutes to get your head around but it's worth it. Then try an array of arrays of dictionaries!...
h4xxr
+1  A: 

You can do the sorting you require with a change to your underlying data structure. Consider using an array of arrays, e.g.:

{
    {3.0, Index 0},
    {2.0, Index 1},
    {3.5, Index 2},
    {1.0, Index 4}
}

and given a sorting function:

NSComparisonResult customCompareFunction(NSArray* first, NSArray* second, void* context)
{
    id firstValue = [first objectAtIndex:0];
    id secondValue = [second objectAtIndex:0];
    return [firstValue compare:secondValue];
}

You can sort it like so:

NSArray* myArray = [NSArray arrayWithObjects:
                    [NSArray arrayWithObjects:
                     [NSNumber numberWithFloat:3.0f],
                     [NSNumber numberWithInt:0], nil],
                    [NSArray arrayWithObjects:
                     [NSNumber numberWithFloat:2.0f],
                     [NSNumber numberWithInt:1], nil],
                    [NSArray arrayWithObjects:
                     [NSNumber numberWithFloat:3.5f],
                     [NSNumber numberWithInt:2], nil],
                    [NSArray arrayWithObjects:
                     [NSNumber numberWithFloat:1.0f],
                     [NSNumber numberWithInt:4], nil]];

NSArray* sortedArray = [myArray sortedArrayUsingFunction:customCompareFunction context:NULL];

NSLog(@"Sorted array: %@", sortedArray);

Which prints:

Sorted array: ( ( 1, 4 ), ( 2, 1 ), ( 3, 0 ), ( 3.5, 2 ) )

Shaggy Frog