Reputation: 2121
I have a Person
object. and this person object has the following attributes;
Name
StartDate
EndDate
I am saving this Person
objects to an array. This array might contain more than 100 person objects.All of the above attributes are Strings.
The following is an example of person objects in that array;
John, Tue Feb 22, Thr Mar 30
Jack, Wed Mar 09, Fri Apr 21
Jack, Thu Mar 19, Fri Dec 20
Jack, Tue Jan 08, Fri Apr 26 etc..
Now i need to will supply a date, say for example Wed 29 Mar
, and i need to check if it's in the range of StartDate
and EndDate
in the array of persons object. I have a pseudo-code for the scenario.
The date i am going to check against the StartDate
and EndDate
are Strings too.
Pseudo-code:
if Startdate >= providedDate && EndDate <= providedDate {
// Add to an Array
else
//Do not add to an array
Since, the StartData
, EndDate
and ProvidedDate
are all Strings how can i check if its Greater than or lesser than the provided date ?
Note: I need an approach that doesn't use NSPredicate
Upvotes: 0
Views: 185
Reputation: 47729
In order to compare values you need values that are comparable. You could use NSDate objects, you could cast dates into sortable character/numeric form (eg 20120425), or you could use custom objects that implement their own compare:options:
methods.
Or you could write a separate compareString:string1 toString:string2
method and use the strings as you have them.
You just have to make up your mind which you want to do.
Upvotes: 1
Reputation: 5435
What are you being tested on here? It'd odd to have a date stored as a string. Are you meant to parse the string dates in order to compare them to the provided date? If so, I'll stay away from any kinds of date classes.
If so, I'd just split it into 3 strings -- day of the week, month, day. You don't care about day of the week.
You don't have a year so you can't do any logic about that... I guess just assume it's all in the same year. (You could do something crazy like calculate 'which recent year did this day fall on this day of the week', but come on...)
Associate each month with an integer. Let's say, using a hash map. As in Months.put('January',1), etc.
Get the number out of each date -- just convert from a string to an integer.
Then the logic for whether something is before or after a date is (if -1 is myMonth is before, 0 is they're the same, and 1 is myMonth is after)
if (Months.get(myMonth) < Months.get(providedMonth)) return -1
else if (Months.get(myMonth) > Months.get(providedMonth)) return 1
else
if (myDate < providedDate) return -1;
else if (myDate > providedDate) return 1;
else return 0;
Upvotes: 1