Hi,

I am creating a program for television shows/programs. everything works fine...time taken by a particular program, total time wasted in a day, week and used in day, week, specific hours etc.

i am having problem during data entry. after the user clicks "Add Program", it is added to a listview. my problem now is this:

say the user enters starting time of a program to be 8:00, and next program at 9:00...after all are added, he may enter 7:30 or 7:45. i need this hour to be added before 8:00 in the listview. so far my codes haven't been able to search for the right "position" to place it in the listview.

any idea please?

Dani AI

Generated

Nice work getting the insertion to behave — ’s scan-and-compare approach is simple and effective for short lists, and ’s reminder to mark the thread solved is appreciated. A couple of practical suggestions to make the solution more robust and future‑proof, and to address alternatives mentioned by .

Always convert the user string into a real Date/Time value before comparing. Implicit string-to-date conversion depends on system locale and can silently mis-interpret input; use explicit parsing (CDate/TimeValue or a controlled parse routine) and then use the numeric serial (CDbl) for comparisons. If you attach a numeric sort key to each row, avoid packing a long composite integer that can overflow a 32-bit ItemData. Prefer a Double serial (kept in a parallel array/dictionary keyed by the ListItem index) or store a lexicographically sortable string like Format$(dt, "yyyyMMddHHnnss") in a hidden subitem.

For larger lists, replace the linear scan with a binary search to find the insertion index (O(log n) instead of O(n)). Keep your numeric keys in a sorted array or list and update that structure on insert/delete; use the binary-search result to call the ListView insert at that index. Example binary-search helper (VB6-style):

Function FindInsertIdx(keys() As Double, ByVal n As Long, ByVal key As Double) As Long
    Dim lo As Long, hi As Long, mid As Long
    lo = 1: hi = n
    Do While lo <= hi
        mid = (lo + hi) \ 2
        If keys(mid) < key Then
            lo = mid + 1
        Else
            hi = mid - 1
        End If
    Loop
    FindInsertIdx = lo
End Function

A few final tips: decide how you want to handle identical start times (stable insert before/after), normalize display to 24‑hour format for clarity, validate user input, and update your auxiliary key store whenever items are removed or reindexed. These adjustments keep insertion correct, fast, and resilient to locale/year issues.

Recommended Answers

All 4 Replies

Try storing an integer containing yymmddhhmm in the ItemData for each ListView entry. It's clunky but it works. Just search down the ItemData until you find the index you want, then

listview.additem "youritem", indexYouFound  
listview.itemdata(listview.newindex) = yymmddhhmm.

Oh, and be sure to use 24-hr clock settings instead of 12-hr.

thanks but am working on listview.

Oh, I finally got the solution:

Private Sub InsertProgramTime(ByVal pgt As String)
Dim looper3 As Integer
Dim cnt As Integer
cnt = lvtt.ListItems.Count
If cnt = 0 Then
programposition = 1
Exit Sub
End If

For looper3 = 1 To lvtt.ListItems.Count

   If DateDiff("s", lvtt.ListItems(looper3).ListSubItems(1).Text, pgt) < 0 Then

   programposition = looper3
   Exit Sub
   Exit For

   End If
Next looper3
programposition = lvtt.ListItems.Count + 1


End Sub
commented: For the effort showed here. +7

Nice in solving this yourself. Some "kudos" for your effort.

Please mark this as solved, found at the bottom of this page "Is this thread solved?", thanks.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.