Rox
Rox

Reputation: 909

iPhone-Sorting Array

I want to sort my array.... In my array elements are in this order dateArray{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19}

But i want this array in reverse order I used this code for reversing..

dateArray=[[NSMutableArray alloc]initWithArray:[[date reverseObjectEnumerator] allObjects]];

But it gives this output

dateArray=(
9,
8,
7,
6,
5,
4,
3,
2,
19,
18,
17,
16,
15,
14,
13,
12,
11,
10,
1,
0
)

But i want this output dateArray(19,18,17,16,15,14,13,12,11,10........2,1,0) I also used sorting method but output is same. how can i do this??? Please help

EDITED:

My array elements are the contents of NSDocument Directory....

SOLVED this question has been sovled. I don't know the exact way to sort my array as i want but i changed my subpaths name. and it gives right output..

Thanks in Advance

Upvotes: 0

Views: 387

Answers (5)

prakhar
prakhar

Reputation: 828

This can be easily done, you can sort your array via NSSortDescriptor but there is a little trick that I will show to you.Here is the code

NSMutableArray *yourArray=[[NSMutableArray alloc] initWithObjects:@"1",@"5",@"10",@"6",@"3",@"11", nil];

NSMutableArray *array=[[NSMutableArray alloc] init];
int count=[yourArray count];
for (int i=0; i<count; i++) {
    [array addObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:[[yourArray objectAtIndex:i]intValue]],@"val", nil]];
}

if([array count] > 0){
    NSSortDescriptor * descriptor = [[[NSSortDescriptor alloc] initWithKey:@"val"
                                                                 ascending:NO] autorelease];
    NSArray * sortedArray = [array sortedArrayUsingDescriptors:
                             [NSArray arrayWithObject:descriptor]];
    [array removeAllObjects];
    [array addObjectsFromArray:sortedArray];
}
[yourArray removeAllObjects];
[yourArray release];
NSLog(@"sorted array=%@",array);

What I am doing in this is that first add your string objects in NSNumber form on a mutable dictionary then add this dictionary into another array.Now use NSSortDescriptor to sort the array in either order ascending or descending.

Upvotes: 1

holex
holex

Reputation: 24041

if you are want to work with strings instead of numbers, you should use the following format for the correct sorting:

20, 19, ... 10, 09, 08, ... 02, 01, 00

therefore, you just have to add a leading 0 to every number. if they are strings, you can sort them like strings only, not like numbers, this is why the leading zero is needed.


by the way the most painless solution is to rename the subpaths then, where the strings come, and you won't need to change anything else in your application for getting the right order.

Upvotes: 1

Hemang
Hemang

Reputation: 27072

I assuming that your array contained objects and not numbers as in your question. Here's an example to sort an array in ascending or descending order.

NSArray *array = [[NSArray alloc]  initWithObjects:@"5",@"1",@"0",@"3",@"2",@"4", nil];

//or

NSArray *array = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:5], [NSNumber numberWithInt:1], [NSNumber numberWithInt:0], [NSNumber numberWithInt:3], [NSNumber numberWithInt:2], [NSNumber numberWithInt:4], nil];


NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:nil ascending:NO]; //YES, for ascending
NSLog(@"Sorted array %@",[array sortedArrayUsingDescriptors:[NSArray arrayWithObjects:sort, nil]]);

Upvotes: 2

LJ Wilson
LJ Wilson

Reputation: 14427

Three ways (of many others, I am sure). Easiest way (thanks to this SO question), is to use the reverseObjectEnumerator:

NSArray *dateArrayReversed = [[dateArray reverseObjectEnumerator] allObjects];

Or you could use a sort descriptor (messy):

NSArray *arrayOne = [NSArray arrayWithObjects:@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13,@14,@15,@16,@17,@18,@19, nil];

NSSortDescriptor* sortOrder = [NSSortDescriptor sortDescriptorWithKey: @"self" ascending: NO];
    NSArray *arrayTwo =  [arrayOne sortedArrayUsingDescriptors: [NSArray arrayWithObject: sortOrder]];

The other way is to create a category for the reversed array. See this SO question for a great example of that.

How can I reverse a NSArray in Objective-C?

Upvotes: 1

Rajneesh071
Rajneesh071

Reputation: 31091

you need to use

-[NSArray sortedArrayUsingSelector:]

or

-[NSMutableArray sortUsingSelector:] and pass @selector(compare:) as the parameter.

Upvotes: 1

Related Questions