1 Reply Latest reply on Apr 5, 2007 6:51 AM by Xtramania

    Comparing Dates

    TJW-dev Level 1
      Hi,

      This is not so much a syntax problem, my lingo is very good usually, more a case of finding the best solution to doing something.

      I have a 2 lingo lists of maybe 1000 - 2000 elements each (dynamically populated)
      Both lists are of the same format.

      Each element of each list represents a "time window" and is organised as a property list e.g.

      [#StartDate:"20070405" , #StartTime:"09:00" , #EndDate:"20070405" , #EndTime:"10:23"]

      All the properties are strings (this is the way I inherited the project by the way), although I could easily convert to another format and then convert back if it was in the interest of the overall speed.

      basically, I need to create a function that will compare the 2 lists and find any "time window" items in list A that overlap any "time window" item in list B. I also need to know in what manner it overlaps:

      There are 7 possible outcomes for this comparison:

      --1. item1 starts before item2 and ends before item2
      --2. item1 starts before item2 and ends within item2
      --3. item1 starts before item2 and ends after item2
      --4. item1 starts within item2 and ends within item2
      --5. item1 starts within item2 and ends after item2
      --6. item1 starts after item2 and ends after item2
      --7. item1 is the same as item2

      I've tried the obvious way of running nested for loops to compare every one, to every other one, but thats over a Million comparisons during each or which I have do the math to convert each one to a useful format and make the actual comparison, which takes a huge amount of time - nearly half an hour on a slow machine.

      Does anybody have an idea for doing this much, much faster, maybe a date based Xtra? or another concept for doing the comparison?

      Many thanks for any ideas.
        • 1. Re: Comparing Dates
          Xtramania
          The generic approach of handling a lot of data is sorting the data before you start make loops over it. Once you have sorted list you can use binary search on it, since you always know that greater items are always to the right from the current one.

          So, if you first create a list of time windows, that is sorted by start date/time and them end date/time, you can search over it fast enough even with Lingo.