Giter Club home page Giter Club logo

Comments (14)

supermarin avatar supermarin commented on September 4, 2024

@tybro0103 sounds good. There's also a (maybe) simpler syntax for the end users, what do you think about this one?

+ (NSArray *)where:(id)condition orderBy:(NSString *)key ascending:(BOOL)ascending;

from objectiverecord.

supermarin avatar supermarin commented on September 4, 2024

Usage:

1st example

[Person where:@{ @"name": @"Steve" } order: @{ @"surname": @"desc" }];

2nd example

[Person where:@{ @"name": @"Steve" } orderBy:@"surname" ascending:NO];

Maybe simpler wasn't the right term, I've meant to say 'more obvious'.

from objectiverecord.

tybro0103 avatar tybro0103 commented on September 4, 2024

Looks alright, only problem is ordering by more than one attribute.

In the AR guides it just shows using a string:
http://guides.rubyonrails.org/active_record_querying.html#ordering

The docs have a hash example:
http://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-order

from objectiverecord.

fiftydegrees avatar fiftydegrees commented on September 4, 2024

@tybro0103 Looks like it's almost possible to specify several attribues with the 1st example provided by @mneorr ; however, it's not possible to define priorities between attributes. Could be a right fix to provide a NSArray with several NSDictionnary.

[Person where:@{ @"name": @"Steve" } order: @[ @{@"surname" : @"desc", @"firstname" : @"asc"} ]];

from objectiverecord.

conradwt avatar conradwt commented on September 4, 2024

@tybro0103 Did you mean the following?

[ Person where: @{ @"name": @"Steve" } order: @[ @{@"surname" : @"desc" }, @{@"firstname" : @"asc"} ] ];

Now, the order clause is properly applied from left to right and the key ordering is maintained within it. However, if you were passing a single NSDictionary, then key ordering wouldn't matter. For example, the following should be possible:

[ Person where: @{ @"name": @"Steve" } order: @[ @{@"surname" : @"desc" } ] ];

will generate the same results as

[ Person where: @{ @"name": @"Steve" } order: @{@"surname" : @"desc" } ];

In short, key ordering starts to matter when you're using 2 or more keys for the order clause.

from objectiverecord.

tybro0103 avatar tybro0103 commented on September 4, 2024

@conradwt yes, looks good ;)

from objectiverecord.

fiftydegrees avatar fiftydegrees commented on September 4, 2024

@conradwt Actually that's what I was thinking about, but I wrote something different. That's the idea to pass an array of dictionaries, thx!

from objectiverecord.

viking2009 avatar viking2009 commented on September 4, 2024

extend fetchWithPredicate:inContext: (sorry for inline code)

// MARK: example: @"name Asc   ,  title desc, amount"
+ (NSArray *)sortDescriptorsFromString:(NSString*)string {
    // if (![sorting isKindOfClass:[string class]] || string.length == 0) return nil;
     NSArray *sorting = [[string split:@","] map:^(NSString *substring) {
         NSArray *tokens = [[substring strip] split:@" "];
        if (tokens.count > 2) return nil;

         NSString *key= [tokens.first strip];
         BOOL ascending = YES;
         if (tokens.count == 2)
            ascending = ![[tokens.last lowercaseString] isEqualToString:@"desc"];

         return [NSSortDescriptor sortDescriptorWithKey:key ascending:ascending];
    }
    return sorting;
}

// MARK: example: @{"  name" : @"desc ", @"title": @"ASC"}
+ (NSArray *)sortDescriptorsFromDictionary:(NSDictionary *)dict {
    // if (![dict isKindOfClass:[NSDictionary class]]) return nil;
     NSArray *sorting = [dict map:^(id key, id value) {
      // if (![key isKindOfClass:[NSString class]]
      //     || ![value isKindOfClass:[NSString class]]) return nil;

          NSString *keyString = [key strip];
        // if (!keyString.length) return nil;
          NSString *sortString =  [value lowercaseString];
          BOOL ascending = ![sortString isEqualToString:@"desc"];

         return [NSSortDescriptor sortDescriptorWithKey:key ascending:ascending];
    }

    return sorting;
}

// MARK: example: @[@" amount",  @{"  name" : @"desc ", @"title": @"ASC"}]
+ (NSArray *)sortDecriptorsFromArray: (NSArray *)array {
    // if (![array isKindOfClass:[NSArray class]]) return nil;
     NSArray *sortDescriptors = [array map:^(id object) {
        if ([object isKindOfClass:[NSString class]]) {
           return [self sortDescriptorsFromString:object];
        } else if ([object isKindOfClass:[NSDictionary class]]) {
           return [self sortDescriptorsFromDictionary:object];
        }
     }

    return [sortDescriptors flatten];
}

+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
                                 inContext:(NSManagedObjectContext *)context {
   [return  self fetchWithPredicate:predicate
                                    orderBy:nil
                                 inContext:context];
}

+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
                                    orderBy:(id)sorting
                                 inContext:(NSManagedObjectContext *)context {

    NSFetchRequest *request = [self createFetchRequestInContext:context];
    [request setPredicate:predicate];

   if ([sorting isKindOfClass:[NSString class]]) {
         [request setSortDescriptors:[self sortDecriptorsFromString:sorting];
   else if ([sorting isKindOfClass:[NSDictionary class]]) {
         [request setSortDescriptors:[self sortDecriptorsFromDictionary:sorting];
   else if ([sorting isKindOfClass:[NSArray class]])
         [request setSortDescriptors:[self sortDecriptorsFromArray:sorting];

    NSArray *fetchedObjects = [context executeFetchRequest:request error:nil];
    return fetchedObjects.count > 0 ? fetchedObjects : nil;
}

from objectiverecord.

viking2009 avatar viking2009 commented on September 4, 2024

NOTE: every dictionary should have only one object, because search result depends on sorting keys order (NSDictionary isn't ordered collection)

from objectiverecord.

supermarin avatar supermarin commented on September 4, 2024

@viking2009 hope you don't mind editing your comment, I've just applied some code formatting.
Whenever you embed inline code, you may want to surround it like this:

// code here

from objectiverecord.

viking2009 avatar viking2009 commented on September 4, 2024

OK, thanks

from objectiverecord.

viking2009 avatar viking2009 commented on September 4, 2024

and one more thing

+ (NSArray *)sortDecriptorsFromArray: (NSArray *)array {
    // if (![array isKindOfClass:[NSArray class]]) return nil;
     NSArray *sortDescriptors = [array map:^(id object) {
//
         else if ([object isKindOfClass:[NSSortDescriptor class]]) {
           return object;
         } else {
            return nil;
         }
//

+ (NSArray *)fetchWithPredicate:(NSPredicate *)predicate
                                    orderBy:(id)sorting
                                 inContext:(NSManagedObjectContext *)context {
//
   else if ([sorting isKindOfClass:[NSSortDescriptor class]])
         [request setSortDescriptors:@[sorting]];
//

from objectiverecord.

viking2009 avatar viking2009 commented on September 4, 2024
// MARK: example: @{"  name" : @"desc ", @"title": @"ASC"}
+ (NSArray *)sortDescriptorsFromDictionary:(NSDictionary *)dict {
    // if (![dict isKindOfClass:[NSDictionary class]]) return nil;
    if ([dict count] != 1) return nil;

from objectiverecord.

supermarin avatar supermarin commented on September 4, 2024

this is in master

from objectiverecord.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.