2

In one column of a table view, I have a list of names that are by default sorted alphabetically. But then I have a column of values that correspond to the names, and when I sort those values instead of alphabetically, ascending or descending, I have several values that are the same.

For example a few rows of 5, 4 and 3. I have seven names that have the value of 5, ten of them for 4, and five of them for 3. What I want to do is sort those seven, five and ten names with the value of 5,4 and 3 alphabetically. Instead of John, Amy, Sue, Beth, Karen, I want it to be Amy, Beth, John, Karen, Sue.

So I sort each name that has the same value alphabetically.

I am stuck on this and cannot figure it out.

Cokile Ceoi
  • 1,326
  • 2
  • 15
  • 26
  • 1
    You need to post some code showing what you've tried. Have you implemented a data source? Did you try sourcing whatever your data source is reading the data from? – bbum Jul 20 '16 at 16:25
  • In your compare function used in the sorting, compare the values first. If they are equal, then compare the names. – fishinear Jul 20 '16 at 16:44

1 Answers1

1

Try this

NSArray *array=@[@"John",@"Amy",@"Sue",@"Beth",@"Karen"];

NSArray *sortedArray=[array sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

NSLog(@"%@",sortedArray);

Answer:

(
Amy,
Beth,
John,
Karen,
Sue
)
Saraswati
  • 1,516
  • 1
  • 14
  • 21