• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Sorting
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Sorting


  • Subject: RE: Sorting
  • From: "Peter Karlsson" <email@hidden>
  • Date: Wed, 16 Feb 2005 23:02:42 +0100

It seems that I'm doing something wrong here. This will not sort
numerically.

The result is:

aaa10
aaa14
aaa15
aaa2

But I want it to be:

aaa2
aaa10
aaa14
aaa15

What am I doing wrong?

	NSMutableArray *arr = [NSMutableArray array];

	NSMutableDictionary *dic1 = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"aaa10",@"key1",@"bbb1",@"key2",@"ccc1",@"key3
",nil];
	NSMutableDictionary *dic2 = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"aaa2",@"key1",@"bbb2",@"key2",@"ccc2",@"key3"
,nil];
	NSMutableDictionary *dic3 = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"aaa14",@"key1",@"bbb3",@"key2",@"ccc3",@"key3
",nil];
	NSMutableDictionary *dic4 = [NSMutableDictionary
dictionaryWithObjectsAndKeys:@"aaa15",@"key1",@"bbb4",@"key2",@"ccc4",@"key3
",nil];

	[arr addObject: dic1 ];
	[arr addObject: dic2 ];
	[arr addObject: dic3 ];
	[arr addObject: dic4 ];

	NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:
@"key1" ascending: YES selector: @selector( compareNumerically: )];

	NSLog([arr description]);
	NSLog([[arr sortedArrayUsingDescriptors:[NSArray
arrayWithObject:descriptor]] description]);

- (NSComparisonResult)compareNumerically:(NSString *)string
	{
		return [self compare:string options:(NSNumericSearch |
NSCaseInsensitiveSearch)];
	}

Peter

Ursprungligt meddelande

> See NSSortDescriptor initWithKey:ascending:selector:
>
> Normally NSSortDescriptor will just call compare: on both objects.
> Using the method above you can tell it to call a different compare
> method. So...
>
> sd = [[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES
> selector: @selector( compareNumerically: )];
>
> Will do what you want assuming you've implemented compareNumerically
> for the objects in the array.
>
> Guy
>
>
> On Wed, 16 Feb 2005 15:23:39 +0100, Peter Karlsson <email@hidden>
wrote:
> > I found a nice example at this url:
> >
> >
http://www.oomori.com/cocoafw/Foundation/NSMutableArray/sortUsingDescriptors
> > .html
> >
> > I have tried this and it works except one thing. This is the result:
> >
> > HRPORG
> > NINA1
> > NINA10
> > NINA8
> > PLKATK
> >
> > I want it to be like this:
> >
> > HRPORG
> > NINA1
> > NINA8
> > NINA10
> > PLKATK
> >
> > That worked when I just sorted strings like this:
> >
> > - (NSComparisonResult)compareNumerically:(NSString *)string
> >         {
> >                 return [self compare:string options:(NSNumericSearch |
> > NSCaseInsensitiveSearch)];
> >         }
> >
> > So my question is, is it possible to use NSNumericSearch in this case?
> >
> > Peter
> >
> > Ursprungligt meddelande
> >
> > > You need to check out:
> > >
> > > NSArray sortedArrayUsingDescriptors
> > >
> > > and
> > >
> > > NSSortDescriptors
> > >
> > >
> > > That should take care of it for you.
> > >
> > > - August
> > >
> > >
> > >
> > > On Feb 16, 2005, at 7:31 AM, Peter Karlsson wrote:
> > >
> > > > Hi!
> > > >
> > > > To read and write the array is not a problem. The problem is how to
> > > > sort
> > > > it. Do you maybe have some example code to show me?
> > > >
> > > > Peter
> > > >
> > > > Ursprungligt meddelande
> > > >
> > > >> Hi Peter,
> > > >>
> > > >> I'm not sure how other people would recommend you do this, but my
idea
> > > >> would be to sort the array in your app (like you have) and then
> > > >> overwrite the array's value in the plist file using the setObject:
> > > >> forKey: method (using the same key, of course).
> > > >>
> > > >> In other words:
> > > >> 1) Read the array from the plist file.
> > > >> 2) Sort the array.
> > > >> 3) Write the array to the plist file using the same key you used to
> > > >> access it.
> > > >>
> > > >> I think that'd be the only way you could do it.
> > > >>
> > > >> Kiel  :-)
> > > >> "Your friend is the person who knows all about you and still likes
> > > >> you."
> > > >> Elbert Green Hubbard
> > > >>
> > > >>  _______________________________________________
> > > >> Do not post admin requests to the list. They will be ignored.
> > > >> Cocoa-dev mailing list      (email@hidden)
> > > >> Help/Unsubscribe/Update your Subscription:
> > > >>
> > > >>
> > > >> This email sent to email@hidden
> > > >>
> > > >
> > > >  _______________________________________________
> > > > Do not post admin requests to the list. They will be ignored.
> > > > Cocoa-dev mailing list      (email@hidden)
> > > > Help/Unsubscribe/Update your Subscription:
> > > >
> > > > This email sent to email@hidden
> > > >
> > >
> > >  _______________________________________________
> > > Do not post admin requests to the list. They will be ignored.
> > > Cocoa-dev mailing list      (email@hidden)
> > > Help/Unsubscribe/Update your Subscription:
> > >
> > > This email sent to email@hidden
> > >
> >
> >  _______________________________________________
> > Do not post admin requests to the list. They will be ignored.
> > Cocoa-dev mailing list      (email@hidden)
> > Help/Unsubscribe/Update your Subscription:
> >
> > This email sent to email@hidden
> >
>  _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Cocoa-dev mailing list      (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Sorting
      • From: "Stan Soria" <email@hidden>
  • Prev by Date: Re: affine transformation of bitmap images
  • Next by Date: NSPopupButton crash
  • Previous by thread: Re: Re: Sorting
  • Next by thread: Re: Sorting
  • Index(es):
    • Date
    • Thread