1

I need to pick from a list the next available date ( if exists ) . If for example I have a list with three dates , namely 12 December 2015 , 3 March 2016 and 25 April 2016, and today is 25 January 2016 . How can I recover only next to the date today ? calculating that unordered lists and I have to retrieve the record next to the current date .

Thank you

Mr. Developer
  • 3,295
  • 7
  • 43
  • 110
  • So in this example you would like to get 3th March 2016? – C4d Jan 25 '16 at 11:15
  • yes in this example 3th March 2016 – Mr. Developer Jan 25 '16 at 11:16
  • Write down the math on paper, then write it in code. The trivial way would be to sort the list based on ABS(milliseconds from now), for example, and take the first result. – CodeCaster Jan 25 '16 at 11:16
  • 1
    Have you tried anything so far? A [mcve] showing the basic setup and how far you've got would help a lot. Hint: if you sort the list, then filter anything before today, it'll be really simple... – Jon Skeet Jan 25 '16 at 11:16
  • Could you clarify what you mean by "next to the date today"? Can it be in the past, or do you only want future dates? – Jon Skeet Jan 25 '16 at 11:17
  • I ordered list , but a bubble sort or like me become very slow , if there is a method of the framework that already does this , and someone in the know it you'll be very happy – Mr. Developer Jan 25 '16 at 11:17
  • @JonSkeet only future and single next date – Mr. Developer Jan 25 '16 at 11:18
  • 1
    Can you anyway please read [ask] and share your research? [Find next closest date](http://stackoverflow.com/questions/22208245/find-next-closest-date), [Find the closest time from a list of times](http://stackoverflow.com/questions/1757136/find-the-closest-time-from-a-list-of-times), [How to find the nearest and the larger from a list of dates?](http://stackoverflow.com/questions/10314156/how-to-find-the-nearest-and-the-larger-from-a-list-of-dates) and so on. – CodeCaster Jan 25 '16 at 11:24

1 Answers1

3

According to the comments this should work for you:

List<DateTime> dt;
dt.Sort((a, b) => a.CompareTo(b)); // If your list isnt sorted.
var greaterThanNow = dt.SkipWhile(x => x <= DateTime.Now).First();

You said it gives you the last element (which in your mind isnt correct). So here's a full example:

List<DateTime> dt = new List<DateTime>()
{
    new DateTime(2016, 01, 20),
    new DateTime(2016, 02, 10)
};
dt.Sort((a, b) => a.CompareTo(b)); // If your list isnt sorted.
var greaterThanNow = dt.SkipWhile(x => x <= DateTime.Now).First();

This will return 10.02.2016 00:00:00.

List<DateTime> dt = new List<DateTime>()
{
    new DateTime(2016, 01, 20),
    new DateTime(2016, 01, 05)
};
dt.Sort((a, b) => a.CompareTo(b)); // If your list isnt sorted.
var greaterThanNow = dt.SkipWhile(x => x <= DateTime.Now).First();

This will give you InvalidOperationException.

List<DateTime> dt = new List<DateTime>()
{
    new DateTime(2016, 01, 20),
    new DateTime(2016, 01, 05)
};
dt.Sort((a, b) => a.CompareTo(b)); // If your list isnt sorted.
var greaterThanNow = dt.SkipWhile(x => x <= DateTime.Now).FirstOrDefault();

This will return 01.01.0001 00:00:00.


I guess this should be enough to continue working.

C4d
  • 3,183
  • 4
  • 29
  • 50
  • i have try this solution but i get the 4th march 2016, is the last element of the list – Mr. Developer Jan 25 '16 at 11:26
  • Is the Sort function better than OrderBy? e.g. `var greaterThanNow = dt.OrderBy(d => d).SkipWhile(x => x <= DateTime.Now).First();` – Jamadan Jan 25 '16 at 11:27
  • @LordJam: Not according the topic but take a look in here: http://stackoverflow.com/questions/1832684/c-sharp-sort-and-orderby-comparison – C4d Jan 25 '16 at 11:50