Sort array in ascending order and remove duplicate values in objective- c

Itaws

I have a horizontally and vertically scrollable table. I get the data for the header and first column from the web service(json). I want to sort the data in ascending order and remove duplicate data from both header and the first column. For removing duplicate values I used the following code:

-(void) requestFinished: (ASIHTTPRequest *) request
{
    NSString *theJSON = [request responseString];

    SBJsonParser *parser = [[SBJsonParser alloc] init];

    NSMutableArray *jsonDictionary = [parser objectWithString:theJSON error:nil];

    headData = [[NSMutableArray alloc] init];
    NSMutableArray *head = [[NSMutableArray alloc] init];

    leftTableData = [[NSMutableArray alloc] init];
    NSMutableArray *left = [[NSMutableArray alloc] init];

    rightTableData = [[NSMutableArray alloc]init];

    for (NSMutableArray *dictionary in jsonDictionary)
    {
        Model *model = [[Model alloc]init];

        model.cid = [[dictionary valueForKey:@"cid"]intValue];
        model.iid = [[dictionary valueForKey:@"iid"]intValue];
        model.yr = [[dictionary valueForKey:@"yr"]intValue];
        model.val = [dictionary valueForKey:@"val"];

        [mainTableData addObject:model];

        [head addObject:[NSString stringWithFormat:@"%ld", model.yr]];
        [left addObject:[NSString stringWithFormat:@"%ld", model.iid]];
    }
    NSOrderedSet *orderedSet = [NSOrderedSet orderedSetWithArray:head];
    headData = [[orderedSet array] mutableCopy];

//    NSSet *set = [NSSet setWithArray:left];
//    NSArray *array2 = [set allObjects];
//    NSLog(@"%@", array2);

    NSOrderedSet *orderedSet1 = [NSOrderedSet orderedSetWithArray:left];
    NSMutableArray *arrLeft = [[orderedSet1 array] mutableCopy];

    //remove duplicate enteries from header array
    [leftTableData addObject:arrLeft];


    NSMutableArray *right = [[NSMutableArray alloc]init];
    for (int i = 0; i < arrLeft.count; i++)
    {
        NSMutableArray *array = [[NSMutableArray alloc] init];
        for (int j = 0; j < headData.count; j++)
        {
            /* NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.iid == %ld", [[arrLeft objectAtIndex:i] intValue]];
             NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];*/
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF.iid == %ld AND SELF.yr == %ld", [[arrLeft objectAtIndex:i] intValue], [[headData objectAtIndex:j] intValue]];
            NSArray *filteredArray = [mainTableData filteredArrayUsingPredicate:predicate];

            if([filteredArray count]>0)
            {
                Model *model = [filteredArray objectAtIndex:0];
                [array addObject:model.val];
            }
        }
        [right addObject:array];
    }
    [rightTableData addObject:right];
}

How will I sort the arrays in ascending order?

Please help.

Itaws

This is what I did to sort the header data in ascending order and to remove duplicates from both header and leftmost column. Hope this will help others

NSOrderedSet *orderedSet3 = [NSOrderedSet orderedSetWithArray:head3];
headData3 = [[orderedSet3 array] mutableCopy];
[headData3 sortUsingComparator:^NSComparisonResult(NSString *str1, NSString *str2)
{
    return [str1 compare:str2 options:(NSNumericSearch)];
}];

NSOrderedSet *orderedSet4 = [NSOrderedSet orderedSetWithArray:left3];
NSMutableArray *arrLeft3 = [[orderedSet4 array] mutableCopy];

[leftTableData3 addObject:arrLeft3];

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sort array in ascending order

From Dev

Sort elements of an array in ascending order

From Dev

Php sort array ascending order

From Dev

Arranging values of array in Ascending order

From Dev

Sorting MultiDimentional NSMutableArray in Ascending Order Objective - C

From Dev

how to sort array of dates in ascending order in ios

From Dev

PHP Sort Varchar Data Array In Ascending Order

From Dev

Sort array by date and time in ascending order

From Dev

How to Sort an array in an ascending order swift 2.3

From Dev

Sort an array in ascending order by using a subroutine in java

From Dev

Sort array by date and time in ascending order

From Dev

How to Sort an array in an ascending order swift 2.3

From Dev

Cannot sort array into ascending order in Python

From Dev

How to sort instances of a class based in ascending order based on the average of the values in an array?

From Dev

How to form ticket fare ascending and descending order form array in objective c

From Dev

Sort 2d array rows in ascending order of their first elements in c#

From Dev

C++:Sort the initial part of an array in ascending and the other part in descending order

From Dev

c++ sort(ascending,descending) integer array

From Java

Sort row values in ascending order without affecting "zero" in R

From Dev

How to sort this float list of numerical values in ascending order?

From Dev

How to sort dictionary's multiple values in ascending order?

From Dev

arrange 2 array on ascending order - C programming

From Dev

how to sort an array how objects in ascending order of x positions

From Dev

How to sort an array in ascending order from Key in PHP?

From Dev

Matlab: sort an array to ascending order in terms of binary interpretation

From Dev

How do I sort these arrays of objects in ascending order in C#?

From Dev

How to sort an IList in ascending or descending order Selenium C#

From Dev

Sorting an array in ascending order

From Dev

Sorting an array in ascending order

Related Related

  1. 1

    Sort array in ascending order

  2. 2

    Sort elements of an array in ascending order

  3. 3

    Php sort array ascending order

  4. 4

    Arranging values of array in Ascending order

  5. 5

    Sorting MultiDimentional NSMutableArray in Ascending Order Objective - C

  6. 6

    how to sort array of dates in ascending order in ios

  7. 7

    PHP Sort Varchar Data Array In Ascending Order

  8. 8

    Sort array by date and time in ascending order

  9. 9

    How to Sort an array in an ascending order swift 2.3

  10. 10

    Sort an array in ascending order by using a subroutine in java

  11. 11

    Sort array by date and time in ascending order

  12. 12

    How to Sort an array in an ascending order swift 2.3

  13. 13

    Cannot sort array into ascending order in Python

  14. 14

    How to sort instances of a class based in ascending order based on the average of the values in an array?

  15. 15

    How to form ticket fare ascending and descending order form array in objective c

  16. 16

    Sort 2d array rows in ascending order of their first elements in c#

  17. 17

    C++:Sort the initial part of an array in ascending and the other part in descending order

  18. 18

    c++ sort(ascending,descending) integer array

  19. 19

    Sort row values in ascending order without affecting "zero" in R

  20. 20

    How to sort this float list of numerical values in ascending order?

  21. 21

    How to sort dictionary's multiple values in ascending order?

  22. 22

    arrange 2 array on ascending order - C programming

  23. 23

    how to sort an array how objects in ascending order of x positions

  24. 24

    How to sort an array in ascending order from Key in PHP?

  25. 25

    Matlab: sort an array to ascending order in terms of binary interpretation

  26. 26

    How do I sort these arrays of objects in ascending order in C#?

  27. 27

    How to sort an IList in ascending or descending order Selenium C#

  28. 28

    Sorting an array in ascending order

  29. 29

    Sorting an array in ascending order

HotTag

Archive