How to: for i=1 to n, for j=i+1 to n?

0
I retrieve a list of objects and need to determine if there are overlapping date ranges in them. Options: (1) Retrieve full list Iterate over entire list Iterate over entire list Compare (2) Retrieve full list 1 ordered by starting date Iterate over list 1 Retrieve list 2 where startdate >= startdate_item1 and id <> id_item1 Iterate over list 2 Compare Which is best for performance? There will (only) be dozens of items in the list. I can abort as soon as overlap is detected. Or can anyone think of a better method?
asked
2 answers
2
    (3)
Retrieve full list 1 ordered by starting date
Iterate over list 1
  Retrieve list 2 where startdate >= startdate_item1 and enddate< enddate_item1 and id <> id_item1
    If not empty set overlap = true and break
  If overlap break
answered
1

Less database calls is usually faster and memory doesn't really seem to be an issue here. I'd go with the double iterating.

answered