Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

.OrderByDescending().ToList() - Where Order by is a Date that might be Null 1

Status
Not open for further replies.

tqeonline

MIS
Oct 5, 2009
304
US
Hey guys!

I hope all is well. I've been working on my first Windows Phone 8 app in my spare time. It is basically a Task/Todo app that consists of Task Lists that contain multiple Tasks. I can sort the Task Lists off the title and that is no issue, the issue comes when I want to sort the Tasks by Due Date.

The due date it stored at Google (Google Tasks) as: 10/01/2012 00:00:00 and when I pull it into my UI I parse it like:

Code:
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var formatString = value as string;
 
            if (!string.IsNullOrEmpty(formatString))
            {
                return DateTime.Parse(formatString).Date.ToShortDateString();
            }
            return "No Due Date";
        }

And that works great. Here is what the function looks like without sorting:

Code:
                try
                {
                    DateTime dateValue;
                    var o = JObject.Parse(response.Content);
                    var lists = new List<TaskItem>();
                    if (o != null && o["items"] != null)
                    {
                        lists =
                            o["items"].Select(
                                m => new TaskItem((string)m.SelectToken("id"),
                                                  (string)m.SelectToken("kind"),
                                                  ((string)m.SelectToken("title")) ==
                                                  String.Empty
                                                      ? "Empty"
                                                      : (string)
                                                        m.SelectToken("title"),
                                                  (string)m.SelectToken("notes"),
                                                  (string)m.SelectToken("parent"),
                                                  (string)
                                                  m.SelectToken("position"),
                                                  (string)m.SelectToken("update"),
                                                  (string)m.SelectToken("due"),
                                                  (string)m.SelectToken("deleted"),
                                                  (string)m.SelectToken("hidden"),
                                                  (string)m.SelectToken("status"),
                                                  (string)
                                                  m.SelectToken("selfLink"),
                                                  (string)
                                                  m.SelectToken("completed")
                                         [COLOR=#73D216])).ToList();[/color]
                    }
                    var tasks = new List<TaskItem>();
                    tasks.Clear();
                    tasks.AddRange(lists);

                    var list = obj[1] as Action<List<TaskItem>>;
                    if (list != null) list(tasks);
                }

Below is what is I have now - it returns errors when the x.due is null.

Code:
[COLOR=#EF2929])).OrderByDescending(x=>DateTime.Parse(x.due)).ToList();[/color]

I have also tried the below and it doesn't like the ??
Code:
[COLOR=#EF2929])).OrderByDescending(x=>DateTime.Parse(x.due) ?? DateTime.MinValue)).ToList();[/color]

Should I be doing this sort somewhere else or is there a way I can capture the null and return MinValue/MaxValue?

- Matt

"If I must boast, I will boast of the things that show my weakness
 
You're problem isn't that DateTime.Parse(null) returns null, its that that doesn't work. You can't parse a null, so you get an exception. You need to check before you try and parse it:

x=> x.due != null ? DateTime.Parse(x.due) : DateTime.MinValue


 
That was it! Thanks Moregelen!

The below is the code for sorting by date where the upcoming items are at the top - and if there is no due date they are put at the top:
Code:
.OrderBy(x=> x.due != null ? DateTime.Parse(x.due) : DateTime.MinValue).ToList();

Change Datetime.MinValue to Datetime.MaxValue and the "Nulls" are pushed to the bottom.

- Matt

"If I must boast, I will boast of the things that show my weakness
 
I've not worked with this specific Google API but I'm sure if you look you can actually include a sort option in your request. Most API I've dealt with include things like that, which is pretty convened. Much easier to programmatically change your request string.
 
Man... iOS autocorrect is making it look like I can't spell...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top