473,408 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

How can I sort a NSArray by NSSortDiscriptor?

Hi All, I am new in iPhone development. I need your help in sorting NSArray.

in this Code I am assigning values to the MyData. this is a NSArray variable.

Expand|Select|Wrap|Line Numbers
  1.  
  2. NSMutableArray *MyArray=[NSMutableArray array];
  3.  
  4. [MyArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:314564454],@"Population", @"Mumbai",@"Name", [NSNumber numberWithInt:415452],@"Area",nil]];
  5.  
  6. [MyArray addObject:[NSDictionary dictionaryWithObjectsAndKeys[NSNumber numberWithInt:454154412],@"Population", @"Chennai",@"Name", [NSNumber numberWithInt:324544],@"Area",nil]];
  7.  
  8. [MyArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:874544541],@"Population", @"Kolkatta",@"Name", [NSNumber numberWithInt:554445],@"Area",nil]];
  9.  
  10. [MyArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:412541245],@"Population", @"Delhi",@"Name", [NSNumber numberWithInt:345475],@"Area",nil]];
  11.  
  12. self.MyData = [NSArray arrayWithArray:MyArray];
  13.  
Now I need top 3 Cities with the Highest Population Information.

So I had Write Following Code.,

Expand|Select|Wrap|Line Numbers
  1. NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"Population" ascending:NO] autorelease]; 
  2. NSArray *sorted = [self.MyData sortedArrayUsingDescriptors: 
  3.     [NSArray arrayWithObject:sorter]];   
  4.  
  5. NSRange range = NSMakeRange(0,3); 
  6. return [sorted subarrayWithRange:range];
  7.  
  8.  
But it is not working. The same code works if I replace the following
Expand|Select|Wrap|Line Numbers
  1. NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"Population" ascending:NO] autorelease]; 
  2.  
With
Expand|Select|Wrap|Line Numbers
  1. NSSortDescriptor *sorter = [[[NSSortDescriptor alloc] initWithKey:@"Name" ascending:NO] autorelease]; 
  2.  
It Works...
Because Name is String. Please help me out How can I sort this array with numeric value.

Thank you in advance.
Jul 29 '10 #1
0 1828

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Ken R. | last post by:
Hello all, I am relatively new to python but I am having an issue with custom sort functions.. I am trying to sort a list of lists or tuples with arbitrary ascending or descending sorts. For...
1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
5
by: neehakale | last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort. I got an idea,in which situation we can use...
5
by: neocortex | last post by:
Hello! I am a newbie in Python. Recently, I get stuck with the problem of sorting by two criteria. In brief, I have a two-dimensional list (for a table or a matrix). Now, I need to sort by two...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.