0

I have 2 NSMutableArray array. First array is storing UserID and second array is storing related UserName. Here is dummy structure of arrays.

First array= {"1","5","2","8"}; Second array={"Akash", "Martin", "Hui", "Ajay"};

I want to sort Second in alphabetical order but respected UserID also should be sort.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Ajay_Kumar
  • 1,359
  • 9
  • 33
  • 60

3 Answers3

3

You should use NSDictionary for this task. Your sorting process will be easier then. You can use key value pair mechanism of NSDictionary like key is ID and value is name.

Rahul Vyas
  • 26,593
  • 48
  • 175
  • 252
1

Duplicate of this question.

This is how you do it, if anArray is your start array:

sortedArray = [anArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

Also, you should use NSDictionary for this, that way when it's sorted, both the keys and values are sorted the same.

Community
  • 1
  • 1
Alex Coplan
  • 12,581
  • 17
  • 72
  • 135
0

If there is no duplicated UserName in the second array, you could: 1. create a Map 2. put UserName array and UserID array to the Map with UserName -> UserID 3. sort the second array, the default order is alphabatical order. 4. sequentially access each element in the second array and get the related UserID from the map and rearrange the first array.

ControlPower
  • 610
  • 4
  • 7