0

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

Popeye
  • 11,839
  • 9
  • 58
  • 91
Rox
  • 909
  • 11
  • 31
  • It seems to be sorting based on `NSString` not `NSNumber`. I would suggest you use something like `sortUsingComparator:`. – Mazyod Aug 31 '12 at 09:50
  • 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. – holex Aug 31 '12 at 10:23
  • @holex actually i m fetching element from document directory. elements are the subpaths of directory. I can't rename in 00,01,02. – Rox Aug 31 '12 at 10:28
  • 1
    @Rox,if they are strings, you can sort them like strings, not numbers... by the way in that case rename the subpaths then. who designed the application's architecture? – holex Aug 31 '12 at 10:37
  • and then how to sort them as string??? – Rox Aug 31 '12 at 10:38
  • @holex Thanx holex i followed ur advice and rename the subpaths. now i gives that output which i want, thanks 4 that..... – Rox Aug 31 '12 at 10:52
  • @holex yes, sure why not... make answer. I'll accept it.. – Rox Aug 31 '12 at 10:55

5 Answers5

2

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]]);
Hemang
  • 26,840
  • 19
  • 119
  • 186
  • @Rox, see this edited data! It works for me! Let me know whats the type of your data, are they objects (strings) or of type number? – Hemang Aug 31 '12 at 10:07
  • It is sorting your elements but if ur elements from 1 to 20 then 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 in series like (20,19,.... 2,1,0) – Rox Aug 31 '12 at 10:10
  • @Rox, It's working for number as well. Don't know why its not suit your need! In which format its returning from NSDocumentDirectory. If its in NSDictionary, you need to pass `key` of that field in `NSSortDescriptor`. – Hemang Aug 31 '12 at 10:14
  • BTW [NSNumber numberWithInt:1]; is the same as @1; – LJ Wilson Aug 31 '12 at 10:15
  • NSArray *date = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:dateDir error:nil]; I am using this code for retrieving elements of directory and storing in NSArray – Rox Aug 31 '12 at 10:17
1

you need to use

-[NSArray sortedArrayUsingSelector:]

or

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

Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
1

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?

Community
  • 1
  • 1
LJ Wilson
  • 14,445
  • 5
  • 38
  • 62
1

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.

holex
  • 23,961
  • 7
  • 62
  • 76
1

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.

prakhar
  • 828
  • 7
  • 18