0

I am getting some data from api.I have to sort date.let me give example in an array where is 10 dictionary in that i have parameter called start time.I have to show lates event first flowed next coming event like today event first and tomorrow event next.

I gone through lot of example I cant understand that can some one help me.

this the result coming from api .

(
        {
        bookingTime = "<null>";
        clubAddress = "Grand by GRT Hotels, Lobby Level, Sir Thyagaraya Road, Thyagaraya Nagar , Chennai";
        clubCity = "Chennai,India";
        clubFacilities = "<null>";
        clubInfoID = 270;
        clubLatitude = "13.0402612";
        clubLoc = Chennai;
        clubLongitude = "80.2423";
        clubMobileNumber = "<null>";
        clubName = "The Code-Grand by GRT Hotels";
        clubType = "Night club";
        clubberMobile = "<null>";
        clubberName = "<null>";
        count = 0;
        coupleCover = 0;
        coupleEntry = 0;
        couplePayment = "<null>";
        description = "<null>";
        distance = 0;
        djId = "<null>";
        dressCode = "<null>";
        endDate = "2017-07-03 18:00:00";
        entryCancellationTime = "<null>";
        eventOffer = "<null>";
        eventType = DANCE;
        favoriteType = NO;
        festivalList = "<null>";
        "idclub_event" = 6042;
        moodDJ = "<null>";
        moodMusic = "Salsa, Bachata, Kizomba";
        moodTheme = "LATIN CODE NIGHT";
        offerCredits = 0;
        paidStatus = NOTPAID;
        personAllowed = 0;
        pictureType = IMAGE;
        rateAmbience = 0;
        rateAvg = 0;
        rateFood = 0;
        rateMusic = 0;
        rateService = 0;
        rsvpEntryTime = "<null>";
        rsvpRequestTime = "<null>";
        rsvpStatus = NO;
        rsvpType = BOTH;
        stagCover = 0;
        stagEntry = 0;
        stagPayment = "<null>";
        startDate = "2017-07-03 15:30:00";
        stopEdit = "<null>";
    },
        {
        bookingTime = "<null>";
        clubAddress = "309 Rajiv Gandhi Salai (OMR), Sholinganallur, Chennai, Tamil Nadu 600119";
        clubCity = "Chennai,India";
        clubFacilities = "<null>";
        clubInfoID = 276;
        clubLatitude = "12.9056392";
        clubLoc = Sholinganallur;
        clubLongitude = "80.2256728";
        clubMobileNumber = "<null>";
        clubName = "The Gateway Hotel";
        clubType = Pub;
        clubberMobile = "<null>";
        clubberName = "<null>";
        count = 0;
        coupleCover = 0;
        coupleEntry = 0;
        couplePayment = "<null>";
        description = "<null>";
        distance = 0;
        djId = "<null>";
        dressCode = "<null>";
        endDate = "2017-09-23 16:00:00";
        entryCancellationTime = "<null>";
        eventOffer = "<null>";
        eventType = DANCE;
        favoriteType = NO;
        festivalList = "<null>";
        "idclub_event" = 5945;
        moodDJ = "<null>";
        moodMusic = "Bachata ";
        moodTheme = "Toke D Keda: Bachata Concert";
        offerCredits = 0;
        paidStatus = NOTPAID;
        personAllowed = 0;
        pictureType = IMAGE;
        rateAmbience = 0;
        rateAvg = 0;
        rateFood = 0;
        rateMusic = 0;
        rateService = 0;
        rsvpEntryTime = "<null>";
        rsvpRequestTime = "<null>";
        rsvpStatus = NO;
        rsvpType = BOTH;
        stagCover = 0;
        stagEntry = 0;
        stagPayment = "<null>";
        startDate = "2017-09-23 10:00:00";
        stopEdit = "<null>";
    },

please help me to sort this,thaks for quick responce.

vadian
  • 232,468
  • 27
  • 273
  • 287

2 Answers2

2

Parse api response and store the event details in a model class. Finally you will get Array of Events

Then sort the array by using NSSortDescriptor

//startDate key is the variable name of your `Event` model. Where you stored the event dates
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES];

// ascending YES/NO based on your needs

NSArray *sortedEventsList = [arrayOfEvents sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

Sort dictionary by date

NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; 
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 

NSMutableArray *dateArray = [[NSMutableArray alloc] init]; 
for (int i=0; i<list.count; i++) { 
NSMutableDictionary *dict = [list[i] mutableCopy]; 
NSString *datestr=dict[@"startDate"]; 
NSDate *date = [formatter dateFromString:datestr]; 

dict[@"startDate"] = date; 
dateArray[i] = dict; 
} 
NSLog(@"%@",dateArray); 
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"startDate" ascending:YES];
Subramanian P
  • 4,188
  • 1
  • 17
  • 25
-1

I create a class

public class MyOwnModel
        {
            public int ID { get; set; }
            public string Header { get; set; }
            public string Body { get; set; }
            public bool Active { get; set; }
        }

and I am getting my Data as a json

    [{"ID":1,"Header":"First Notifications","Body":"Body of alert","Active":true}
,{"ID":2,"Header":"Second Notifications","Body":"Body 2","Active":true}
,{"ID":3,"Header":"Third Notifications","Body":"Body 3","Active":true}
,{"ID":4,"Header":"Fourth Notification","Body":"Body 4","Active":true}]

before sorting

and here after the sorting you see the first item with ID 4 After Sorting

How I did it I just used System.Linq;

var objSorted = from a in obj orderby a.ID descending select a;

you can replace id with Date and since you want to sort today then tomorrow this should be ascending when you get the data you make sure you getting data of Today and up not yesterday then the sort will be correct ascending Good luck

khaled Dehia
  • 364
  • 2
  • 8
  • Sorry I didn't see the Tag , sure here is the answer https://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it change the birthdat to Date NSSortDescriptor *sortDescriptor; sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthDate" ascending:YES]; NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:@[sortDescriptor]]; – khaled Dehia Jul 03 '17 at 17:41