INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

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

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

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

(OP)
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 --> C#

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 --> C#

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")
                                         )).ToList();
                    }
                    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 --> C#

)).OrderByDescending(x=>DateTime.Parse(x.due)).ToList(); 

I have also tried the below and it doesn't like the ??

CODE --> C#

)).OrderByDescending(x=>DateTime.Parse(x.due) ?? DateTime.MinValue)).ToList(); 

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"

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

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


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

(OP)
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"

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

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.

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

Man... iOS autocorrect is making it look like I can't spell...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close