Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6086739
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:47:24+00:00 2026-05-23T11:47:24+00:00

I have a CoreData entity with two attributes. One called ‘position’ the other called

  • 0

I have a CoreData entity with two attributes. One called ‘position’ the other called ‘positionChange’. Both of them are integers where the position attribute is the current position and the positionChange is the difference between the previous position and the new position. This means that the positionChange can be negative.

Now I would like to sort by positionChange. But I would like it to disregard the negative values. Currently I’m sorting it descending, which will give the result: 2, 1, 0, -1, -2.
But what I’m looking for is to get this result: 2, -2, 1, -1, 0.

Any ideas on how to solve this using sort descriptors?


EDIT

I got 2 classes, one called DataManager and the other containing my NSNumber category (positionChange is of type NSNumber).

In DataManager I have a method called ‘fetchData:’ where I’m executing my fetch request with a sort descriptor:

NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Entity" inManagedObjectContext:managedObjectContext];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"positionChange" ascending:NO selector:@selector(comparePositionChange:)];
[request setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];

I’m doing some more stuff to the request, but that’s not interesting for this issue.

My NSNumber category should be exactly like the one you posted:
In the .h:

@interface NSNumber (AbsoluteValueSort)
- (NSComparisonResult)comparePositionChange:(NSNumber *)otherNumber;
@end

And in the .m:

@implementation NSNumber (AbsoluteValueSort)

- (NSComparisonResult)comparePositionChange:(NSNumber *)otherNumber
{
    return [[NSNumber numberWithFloat:fabs([self floatValue])] compare:[NSNumber numberWithFloat:fabs([otherNumber floatValue])]];
}

@end

When I call fetchData on my DataManager object I get this error:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'unsupported NSSortDescriptor selector: comparePositionChange:'

Any ideas of what might be the case? I have included my NSNumber category header file in my DataManager class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:47:25+00:00Added an answer on May 23, 2026 at 11:47 am

    Try this, assuming your positionChange is an NSNumber:

    @interface NSNumber (AbsoluteValueSort)
    
    -(NSComparisonResult) comparePositionChange:(NSNumber *)otherNumber;
    
    @end
    

    followed by:

    @implementation NSNumber (AbsoluteValueSort)
    
    -(NSComparisonResult) comparePositionChange:(NSNumber *)otherNumber
    {
        return [[NSNumber numberWithFloat: fabs([self floatValue])] compare:[NSNumber numberWithFloat: fabs([otherNumber floatValue])]];
    }
    
    @end
    

    and then do this:

    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"positionChange" ascending:NO selector:@selector(comparePositionChange:)];
    

    and sort with that descriptor. If you want to make it so that -2 is always after 2, or 5 is always after -5 you can modify comparePositionChange: to return NSOrderedAscending in the cases when one number is the negative of the other.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have only one entity, which has two attributes: an NSString attribute and a
I have an entity in core data that has 2 Attributes. One that is
I have an Entity Item in Core Data. Item has two attributes: name and
When I have a CoreData entity named, say, 'Book', which has a one-to-one relationship
I have an Core Data Entity with a number of attributes, which include amount(float),
Consider that i have two entities with following relationship: Entity A <-->> Entity B
I have the following core data model with two entities: entity item which holds
I have a CoreData entity (waypoint) with a foreign key to another entity (track).
I have a core data model with an entity called clients that is made
I have a simple core-data entity that has a Boolean attribute called subscribedToNewsletter .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.