0

I have an array and a tableview that gets its data from the array. When I view the tableview on the iPhone, its, ofc, not sorted, and its listed as I have written them.

I wanna sort my array in the tableview, so they aren't listed like this:

  • D
  • A
  • G
  • F
  • S

but listed like this instead:

  • A
  • B
  • C
  • D
  • E
  • Etc...

How can I do this?

Here is my array:

stationList = [[NSMutableArray alloc] init];
[stationList addObject:@"A"];
[stationList addObject:@"B"];
[stationList addObject:@"C"];

How do I load the array into my tableview array? Loading the array:

NSString *cellValue = [stationList objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
1
  • 1
    Read the API doc for NSMutableArray - there are methods for sorting. Or Google for "sort nsmutablearray". Commented Mar 27, 2011 at 2:16

3 Answers 3

4

Use [stationList sortUsingFunction:compareLetters context:whateveryouwant_ornil]

And

NSComparisonResult compareLetters(id t1, id t2, void* context) {

      do the tests you want beetween t1 and t2, and return the result
      like :

      if (the test) return NSOrderedDescending, NSOrderedAscending or NSOrderedSame
}

In your example, you should do :

NSMutableArray stationList = [[NSMutableArray array];
[stationList addObject:@"A"];
[stationList addObject:@"B"];
[stationList addObject:@"C"];
[stationList sortUsingFunction:compareLetters context:nil];

NSComparisonResult compareLetters(id t1, id t2, void* context) {
      return [t1 compare:t2];
}
Sign up to request clarification or add additional context in comments.

12 Comments

what do you mean with "do the tests you want beetween t1 and t2, and" ?? what should i do test for ? sorry if its a stupid question
@Patrick R : this depends on the things you want to sort. If you sort strings, call [t1 compare:t2], if you test integers, test t1 == t2, if you test custom objects, you're the only one that know what must be tested, like t1.age and t2.age, ...
@Patrick R : the compare function will be automaticaly be called for mutiple pairs of your array values, untill the whole thing is sorted. You just need to test and return how the 2 params are sorted each other
im testing for strings.. soo.. it needs to be "do [t1 compare:t2]" ? sorry, but im not that into objective-c :/
@Patrick R : see my edit. the compare function beetween NSString already return NSOrderedDescending, NSOrderedAscending or NSOrderedSame. So it's easy. For such a simple case, I believe there is a more simplier way but this one works, and can be reused for other type of contents. Do you know the AppKiDo software ? If you don't you should take a look at it, it would be a great help.
|
2

Go to the below tutorial for sorting NSArray, they have explained the basic technique to sort a NSArray.

You can also download the sample code provided in the end of tutorial. Here is the link.

Comments

0

You can user some simple steps. Which I am using. I am not sure its perfect in context of memory consumption. But its working with me in my application.

Here record is NSMutableArray. Columnname is on Which Coulmn you want sorting. In my case I had more than one column to sort. And Sorting takes BOOL data.

After soring I am reloading my TableView.



    NSSortDescriptor * frequencyDescriptor = [[NSSortDescriptor alloc] initWithKey:columnName ascending:YES];

    NSArray * descriptors = [NSArray arrayWithObjects:frequencyDescriptor, nil];
    [record sortUsingDescriptors:descriptors];
    [columnName release];
    [frequencyDescriptor release];
    [myTableView reloadData];   



I hope this is fine. Do let me know if I can make it better

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.