Results 1 to 2 of 2

Thread: List Sorting conundrum (try at your peril)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Location
    Brisbane, AUS
    Posts
    14

    List Sorting conundrum (try at your peril)

    Ok, I have been attempting this problem for 2 solid weeks now and have to admit it's driving me insane.

    Here is the data:

    Code:
      RowText AlloIndex   PosIndex    FromTime            ToTime        OverLap
        test1      0           0    21/06/2022 14:42    22/06/2022 2:43     0
        test2      0           0    21/06/2022 14:42    22/06/2022 2:43     1
        test3      0           0    22/06/2022 2:42     22/06/2022 14:43    0
        test4      0           0    22/06/2022 14:42    23/06/2022 2:43     0
        test5      5           5    21/06/2022 19:42    22/06/2022 7:43     0
        test6      6           6    21/06/2022 16:42    22/06/2022 4:43     0
        test7      7           7    21/06/2022 15:42    22/06/2022 3:43     0
        test8      7           7    21/06/2022 19:42    22/06/2022 7:43     1
        test9      8           8    21/06/2022 21:42    22/06/2022 9:43     0
        test10     8           8    21/06/2022 22:42    22/06/2022 10:43    1
        test11     9           9    22/06/2022 0:42     22/06/2022 12:43    0
        test12     9           9    21/06/2022 15:42    22/06/2022 3:43     1
        test13     9           9    22/06/2022 12:42    23/06/2022 12:43    1
        test14     9           9    22/06/2022 10:42    23/06/2022 10:43    1
        test15    10          10    21/06/2022 20:42    22/06/2022 8:43     0
        test16    10          10    22/06/2022 1:42     22/06/2022 13:43    1
    What I am attempting to do is, if there is a time overlap in the same **AlloIndex** group, increase the **PosIndex** of the overlaps by 1 (those identified as an overlap are those greater in time in the same **AlloInndex** group that overlap. I have already highlighted these by the **Overlap** column).

    However, this is the tricky part, if an overlap increases the **PosIndex** by 1, all other rows with **PosIndex** values greater than the overlap **PosIndex** are also increased by 1. Example: **PosIndex** 0 has an overlap, so all **PosIndex** greater than 0 also have to increase by 1, in a sense moving them down the rank. **PosIdex** 9, in particular, is very tricky, because you have 4 entries, and 3 overlaps. So you have to increase the **PosIndex** of each overlapped entry by 1 (leaving the 1st entry in the group alone), but also any **PosIndex** greater also needs to increase by 1. Hope this makes sense.

    I have tried **FOR LOOPS**, **LINQ**, and **lambda** to no avail...

    This is the closest I have come to solving this, but it doesn't work when there are more than 2 overlaps in the same **AlloIndex** Group.

    Code:
     For Each bar In lst       
        
            If prevFromDT < bar.ToTime 
              And prevToDT > bar.FromTime 
              And prevIndex = bar.PosIndex Then
        
                bar.overLap = True
        
            End If
        
        next
        
            For Each tsk In lst
        
                If tsk.overLap Then
        
                    For Each tsk1 In lst
        
                        If tsk1.PosIndex > tsk.PosIndex Then 
                            tsk1.PosIndex += 1
        
                        Next
        
                End If
        
            Next
        
            For Each tsk In lst
        
                If tsk.overLap Then
        
                    tsk.PosIndex += 1
        
                End If
        
            Next
    The resulting data should look like this:

    Code:
     RowText	AlloIndex	PosIndex	FromTime	       ToTime	     OverLap
        test1	  0	           0	  21/06/2022 14:42	22/06/2022 2:43	   0
        test2	  0	           1	  21/06/2022 14:42	22/06/2022 2:43	   1
        test3	  0	           0	  22/06/2022 2:42	22/06/2022 14:43   0
        test4	  0	           0	  22/06/2022 14:42	23/06/2022 2:43	   0
        test5	  5	           6	  21/06/2022 19:42	22/06/2022 7:43	   0
        test6	  6	           7	  21/06/2022 16:42	22/06/2022 4:43	   0
        test7	  7	           8	  21/06/2022 15:42	22/06/2022 3:43	   0
        test8	  7	           9	  21/06/2022 19:42	22/06/2022 7:43	   1
        test9	  8	          10	  21/06/2022 21:42	22/06/2022 9:43	   0
        test10	  8	          11	  21/06/2022 22:42	22/06/2022 10:43   1
        test11	  9	          12	  22/06/2022 0:42	22/06/2022 12:43   0
        test12	  9	          13	  21/06/2022 15:42	22/06/2022 3:43	   1
        test13	  9	          14	  22/06/2022 12:42	23/06/2022 12:43   1
        test14	  9	          15	  22/06/2022 10:42	23/06/2022 10:43   1
        test15	  10	      16	  21/06/2022 20:42	22/06/2022 8:43	   0
        test16	  10	      17	  22/06/2022 1:42	22/06/2022 13:43   1

  2. #2

    Thread Starter
    New Member
    Join Date
    Jul 2017
    Location
    Brisbane, AUS
    Posts
    14

    Re: List Sorting conundrum (try at your peril)

    I think i've solved it. Here is the code. Its a bit messy, but it works.

    Code:
         Private Sub addBedOverLapIndex()
        
                Dim i As Integer            
                Dim first As Boolean = True
        
                For Each bar In lstBars
        
                    If (From x In lstBars Where x.FromTime < bar.ToTime And x.ToTime > bar.FromTime And x.PosIndex = bar.PosIndex And x.BookingUID <> bar.BookingUID).Any Then bar.overLap = True
                    If bar.overLap Then
                        Dim test = From x In lstBars Where x.AllocationIndex = bar.AllocationIndex And bar.overLap = True And x.overLap = True
                        If test.Count > 1 Then
                            first = True
                            i = 1
                            For Each all In test
                                If Not first Then
                                    all.BedOverLapIndex = i
                                    i += 1
                                End If
                                first = False
                            Next
                        End If
                    End If
                 
        
                Next
        
            End Sub
        
        
        
            Public Sub sortbars()
        
                Dim j As Integer = 0
                Dim first As Boolean = True
        
                addBedOverLapIndex()
                Dim RemoveFirstOverlap = lstBars.Where(Function(ol1) ol1.overLap = True).GroupBy(Function(x) x.PosIndex, Function(key, g) g.OrderBy(Function(e) e.PosIndex).FirstOrDefault).ToList()
        
                For Each tst In RemoveFirstOverlap
                    tst.overLap = False
                Next
        
                lstBars = (From x In lstBars Select x Order By x.AllocationIndex, x.BedOverLapIndex).ToList
        
        
                For Each s In lstBars
        
                    If s.overLap Then j += 1
        
                    If Not first Then s.PosIndex += j
        
                    first = False
        
                Next

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width