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
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:
You can sort it like so:
Which prints:
Sorted array: (
(
1,
4
),
(
2,
1
),
(
3,
0
),
(
3.5,
2
)
)