display data in alphabetical order iphone - iphone

How to display xml parsed Data in UITableView In alphabetical order.?

In order to display/arrange your data in alphabetical which in a array you have to use NSSortDescriptor
you have to make the object of this NSSortDescriptor class and give data here which you are fetching from XML
NSSortDescriptor *itemXml = [[NSSortDescriptor alloc] initWithKey:#"itemName" ascending:YES];
Now suppose you have an array sortDescriptors
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemXml,nil];
[yourArray sortUsingDescriptors:sortDescriptors];
Now give the yourArray to your UITableView delegate methods...... you will get the sorted data on table

NSSortDescriptor *sorter;
sorter = [[NSSortDescriptor alloc]initWithKey:#"Category" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObject:sorter];
[categoryListArray sortUsingDescriptors:sortDescriptors];
[sorter release];
Try This

Actually there are lots of different ways to sort arrays.
Sort descriptors are only one example.
Others are sortedArrayUsingComparator, sortedArrayUsingFunction:context, sortedArrayUsingSelector, and new to Mac OS 10.6 and iOS 4.0, sortedArrayWithOptions:usingComparator.
Those are NSArray methods that return a sorted array.
NSMutableArray also has variations on those methods that sort the mutable array "in place".

Related

What will be my key in NSSortDescriptor

I want to sort my array in ascending and descending order. For that I have search on Google and thanks to StackOverflow I have got this answer,
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"inputtime" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
But I dont understand what will be the KEY which is in code inputtime. As you see all the data is provided is in NSMUtableArray and NSArray form than what will be key? I have tried to read apple document but still does not understand what is the key. Plz help me on this. Do i have to create dictionary?
You can use this code to sort array.
It helped me, and for this code thanx to #Anoop Vaidya
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:#"self" ascending:NO];
NSArray *descriptors=[NSArray arrayWithObject: descriptor];
NSArray *reverseOrder=[yourAry sortedArrayUsingDescriptors:descriptors];
Key is used when your array contain dictionary or Core data objects.
But when your array doesnt have any objects like this and containing simple NSString objects,you just need to use
"self" in place of key. It will surely work for you
Kane I think it will work for you-
NSArray *sortDescriptors = [NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:#"time" ascending:NO]];
[request setSortDescriptors:sortDescriptors];
Here time is the key on basis of which i am fetching data in descending order. You can define a key on the basis of which you have to fetch. If there is no key then put self in place of key.
The Key will be on the basis of which you want to fetch data as in my example time is the key.
Hope this helps.

Objective-c: how to sort a NSMutableArray of UIButtons using their tags

I have a NSMutableArray containing UIButtons. I have given each of the buttons a unique
tag. The buttons will be added to the array in random order, but i want to later sort it so that the buttons are in ascending order with respect to their tags.
Thanks for the help!
Yeap.
Try this:
NSSortDescriptor *ascendingSort = [[NSSortDescriptor alloc] initWithKey:#"tag" ascending:YES];
NSSortDescriptor *descendingSort = [[NSSortDescriptor alloc] initWithKey:#"tag" ascending:NO];
NSArray *sortedArray = [someArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:ascendingSort]];
You can use the NSSortDescriptor to sort a NSArray.
NSArray *unsortedArray = ...
NSArray *sortedArray = [unsortedArray sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:#"tag" ascending:YES]]];
There are multiple "sortedArrayUsing..." functions. Use the one that seems to fit your needs the best.
(BTW, remember that, since NSMutableArray is a subclass of NSArray, all of the methods of NSArray apply to NSMutableArray as well.)

Sorting Array of Custom Class Objects

I am facing problem that is of sorting. The scenario is explained as: i have a class (Suppose of person) and it has suppose 4 attributes (firstName, lastName, Address, salary) i am creating its object and making a collection of all attributes and putting this object in a NSMutableArray and so on. So at every index of array i have one object (i.e collection of 4 attributes). Now i want to sort that array on the basis of salary can anyone help me out regarding this problem, will be thankful.,
See the documentation for NSArray. It has several sorting methods. Search for the word "sort" and you'll find 'em. (You'll want either the block based or function based API).
use this
NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:#"salary" ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject: sorter];
[yorArrary sortUsingDescriptors:sortDescriptors];
Either you implement a compare-method for your object:
- (NSComparisonResult)compare:(Person *)otherObject {
return [self.birthDate compare:otherObject.birthDate];
}
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingSelector:#selector(compare:)];
or usually even better: (The default sorting selector of NSSortDescriptor is compare:)
NSSortDescriptor *sortDescriptor;
sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:#"birthDate"
ascending:YES] autorelease];
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray;
sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors];

How to sort NSArray of objects based on one attribute

I am trying to sort an array of managed objects alphabetically. The attribue that they need to be sorted by is the name of the object (NSString) with is one of the managed attributes. Currently I am putting all of the names in an array of strings and then using sortedNameArray = [sortedNameArray sortedArrayUsingSelector:#selector(localizedCaseInsensitiveCompare:)]; and then enumerating them back into an array with the objects. This falls apart when two names are the same so I really need to be able to sort by one attribute. How should I go about doing this?
Use NSSortDescriptor. Just search the documentation on it and there some very simple examples you can copy right over. Here is a simplified example:
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:#"MyStringVariableName" ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:valueDescriptor];
NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:descriptors];
And just like that you have a sorted array.
You can do this by using NSSortDescriptor,
eg.
`NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc]initWithKey:#"distance" ascending:YES];`
// Here I am sorting on behalf of distance. You should write your own key.
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor];
NSArray *sortedArray=[yourArray sortedArrayUsingDescriptors:descriptors];`

How can I sort an NSMutableArray alphabetically?

I want to sort an NSMutableArray alphabetically.
You can do this to sort NSMutableArray:
[yourArray sortUsingSelector:#selector(localizedCaseInsensitiveCompare:)];
The other answers provided here mention using #selector(localizedCaseInsensitiveCompare:)
This works great for an array of NSString, however the OP commented that the array contains objects and that sorting should be done according to object.name property.
In this case you should do this:
NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:#"name" ascending:YES];
[yourArray sortUsingDescriptors:[NSArray arrayWithObject:sort]];
Your objects will be sorted according to the name property of those objects.
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:#"name" ascending:YES]; // Describe the Key value using which you want to sort.
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor]; // Add the value of the descriptor to array.
sortedArrayWithName = [yourDataArray sortedArrayUsingDescriptors:descriptors]; // Now Sort the Array using descriptor.
Here you will get the sorted array list.
In the simplest scenarios, if you had an array of strings:
NSArray* data = #[#"Grapes", #"Apples", #"Oranges"];
And you wanted to sort it, you'd simply pass in nil for the key of the descriptor, and call the method i mentioned above:
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:nil ascending:YES];
data = [data sortedArrayUsingDescriptors:#[descriptor]];
The output would look like this:
Apples, Grapes, Oranges
For more details check this
Use the NSSortDescriptor class and rest you will get every thing here
NSSortDescriptor * sortDescriptor;
sortDescriptor = [[NSSortDescriptor alloc] initWithKey:#"Name_your_key_value" ascending:YES];
NSArray * sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSArray * sortedArray;
sortedArray = [Your_array sortedArrayUsingDescriptors:sortDescriptors];
Maybe this can help you:
[myNSMutableArray sortUsingDescriptors:#[[NSSortDescriptor sortDescriptorWithKey:#"firstName" ascending:YES],[NSSortDescriptor sortDescriptorWithKey:#"lastName" ascending:YES]]];
All is acording to NSSortDescriptor...