How to sort UITableview sections - Using NSDictionary - ios

I am using the NSDictionary to show the data in UITableview.
And Sections is now showing like -
My Log is - NSLog(#"%#",[self.base_dictionary allKeys]);
(
"2 days ago",
"2 weeks ago",
"4 weeks ago",
"5 days ago",
"1 week ago",
Today
)
Anyone can help how i can sort it properly -
("Today","2 days ago","5 days ago","1 week ago","2 weeks ago","4 weeks ago")

Change the key that is being used from a string to a numerical string containing the value of number of days since today, i.e. "000","014","028", "128", etc. This can then be used to sort the items, you'll need to ensure you have leading zeros otherwise the sort won't work correctly.
Then when you want to display the title for the section, use a method to calculate a string representation

Related

Calculating the time and converting into Days, Hours and Minutes

I have been trying to convert the subtracted time to days, hours and minutes but my formula is missing with some functionality which i am unable to track.
I would appreciate your help.
=INT(I4)&" days "&TEXT(I4,"h"" hrs ""m"" mins """)
I have attached a Sheet where i have made all these calculation.
google Sheet
You can try with this formula that checks if each value is existent and adds its correspondent suffix:
=ArrayFormula(LAMBDA(dif,IF(D4:D="","",IF(ROUNDDOWN(HOUR(dif)/9)=0,"",ROUNDDOWN(HOUR(dif)/9)&" days ")
&IF(MOD(HOUR(dif),9)=0,"",MOD(HOUR(dif),9)&" hs.")&IF(MINUTE(dif)=0,"",MINUTE(dif)&" mins")))
(E4:E-D4:D+IF(E4:E<D4:D,1,0)))
PS: You can change the 9 to a cell that actually contains the value of the shift, in case it can change

Google sheets formula to calculate hours after a specific time and day

I am new to google sheets, but I have this very simple and basic tracking system for my work schedule. I wish to make a formula so it automatically finds the number of hours on nights and weekends column, after a specific time.
So ex. night hours between 9 pm-11 pm Monday - Friday and weekend hours between 2 pm-8 pm on Saturday and Sunday.
Check if you get the expected hours (sorry for the layout a bit different!)
https://docs.google.com/spreadsheets/d/1YuY_8XgMUsPtBPJlBgxDFgz8JKB3lCmn2e6vVEutvBY/edit?usp=sharing
test if we =WEEKDAY(A4,2)>5
day on monday-friday =if(D4,, max(0,min(21/24,C4)-B4))
night on monday-friday =if(D4, ,max(0,C4-max(21/24,B4)))

show weeks_ago based off week number?

Im trying to show the number of weeks ago calculated from the week number
" x weeks ago "
this should just increment one week ago, two weeks ago, three weeks ago, 1 month ago, 2 months ago, 1 year ago.. etc
Im using method helper:
def weeks_ago_in_words(from_time, include_seconds = false)
to_time = Time.now
weeks_ago = ((to_time - from_time)/1.week).abs
[nil, "last week", "two weeks ago", "three weeks ago"][weeks_ago] ||
distance_of_time_in_words(from_time, to_time, include_seconds)
end
In view:
= weeks_ago_in_words(Time.local(2013) + week.to_i)
week = a week number like 1,10,28,52 etc
This does not give me correct resuls, is there a better way to calculate the "x weeks ago " based on the weeknumer?
If you are comparing two dates from the same year here is the code using cweek:
require 'date'
Time.now.to_date.cweek - Date.new(2013,1,1).cweek
If you are comparing two different years you'll have to add some conditional logic to determine your year multiplier as cweek returns the same value for Dec. 31, 2012 and Jan. 1, 2013.

Convert week number to date range with Saturday as beginning_of_week

I need to use Saturday as the week start and calculate the beginning and the end of week from week numbers. I need week 53 to be properly accounted for as well.
Date.beginning_of_week = :saturday
works fine, but I have not found a way to generate week start and end dates only from a year and week number. Date.commercial is the only method I have been able to use thus far to convert a week number and year only to a date. I have been unable to get Date.commercial to recognize Saturdays as the week start.
I need to use Saturday as the week start and calculate the beginning and the end of week from week numbers.
Given an instance of Date representing the first Saturday, and the week number, this is quite simple, unless I'm missing something.
def beginning_of_week(first_saturday, week_num) {
return first_saturday + (7 * week_num.to_i).days
}
The days method comes from activesupport.

How can I make a `weeks_ago` helper?

How can I make a helper that will tell me how many weeks ago (rounded down) with rails? It could be based of the time_ago_in_words helper however I'd want it to return: "last week" then afterwards just two weeks ago, three weeks ago, etc...
Try this:
def my_time_ago_in_words(from_time, include_seconds = false)
to_time = Time.now
weeks_ago = ((to_time - from_time)/1.week).abs
[nil, "last week", "two weeks ago", "three weeks ago"][weeks_ago] ||
distance_of_time_in_words(from_time, to_time, include_seconds)
end
This function will behave the same as time_ago_in_words. When the from_time is between 1 - 3 week ago, this will print last week, two weeks ago, three weeks ago otherwise it will print the usual.
That would be a nice patch to distance_of_time_in_words:
http://github.com/rails/rails/blob/4cbb9db0a5ff65b0a626a5b043331abefd89e717/actionpack/lib/action_view/helpers/date_helper.rb#L68-103
Also, you can write custom time descriptions for the other levels (day, month, year).
http://robots.thoughtbot.com/post/392707640/the-more-you-know-custom-time-descriptions

Resources