Additional perspective--- Sorting a string file, comapring and displaying only the latest entries


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Additional perspective--- Sorting a string file, comapring and displaying only the latest entries

  1. #1
    Jim Burak Guest

    Additional perspective--- Sorting a string file, comapring and displaying only the latest entries


    Take the following text file for example:

    Ann, any text, 1000
    Bob, any text, 1000
    Charlie, any text, 1001
    Bob, any text, 1011
    Ann, any text, 1023
    Ann, any text, 1045
    Bob, any text, 1100
    Charlie, any text, 1118
    Charlie, any text, 1124

    I have an application that produces a file similar to this (see above).
    The record at the bottom is the most recent event performed by a user. The
    original text file is unsorted and I have that part licked (see below).

    Ann, any text, 1000
    Ann, any text, 1023
    Ann, any text, 1045
    Bob, any text, 1000
    Bob, any text, 1011
    Bob, any text, 1100
    Charlie, any text, 1001
    Charlie, any text, 1118
    Charlie, any text, 1124

    When I display my results I get what you see above. What I can't figure
    out for the life of me (and it seems so simple) is how to compare all occurrences
    of a name and display only the latest entry for each person (see below).

    Ann, any text, 1045
    Bob, any text, 1100
    Charlie, any text, 1124

    Do you see my dilemma? I am going grey!

    "Jim Burak" <jim.burak@cevp.com> wrote:
    >
    >Hello all,
    >
    >I have a question. I am writing something to sort a text file and display
    >only the latest entry. Let me explain a little better. Take the following
    >input file for example:
    >
    >Jim, any text1, 1000
    >Bobby, any text2, 1001
    >Sally, any text3, 1002
    >Jim, any text4, 1214
    >Bobby, any text5, 1222
    >Sally, any text6, 1245
    >
    >I have written the code to sort the file by name and that works just fine.
    > What's kicking my butt right now is displaying only the latest entry.

    The
    >third variable in each line is the time (a string) what I need the code

    to
    >do is after sorting in decending order is to find the two occurences of

    say,
    >Bobby, in the file and then compare the times and display the latest information
    >or "any text5".
    >
    >To recap, I've got the sort down I just need a way to throw out all of the
    >oldest times and display only the latest one.
    >
    >Any thoughts?
    >
    >Thanks in advance!
    >
    >JB
    >
    >Also could all responders also CC jim.burak@cevp.com in their replies?

    I'll
    >see that quicker than going to the website periodically.
    >



  2. #2
    Brad Siemens Guest

    Re: Additional perspective--- Sorting a string file, comapring and displaying only the latest entrie

    Sort Descending on the 1st and 3rd column and ignore any items that begin
    with a column 1 entry that already exists.

    IOW each time the item in column 1 changes add it to the collection (or what
    ever) otherwise ignore it.

    Brad

    "Jim Burak" <jim.burak@cevp.com> wrote in message
    news:3a143c49$1@news.devx.com...
    >
    > Take the following text file for example:
    >
    > Ann, any text, 1000
    > Bob, any text, 1000
    > Charlie, any text, 1001
    > Bob, any text, 1011
    > Ann, any text, 1023
    > Ann, any text, 1045
    > Bob, any text, 1100
    > Charlie, any text, 1118
    > Charlie, any text, 1124
    >
    > I have an application that produces a file similar to this (see above).
    > The record at the bottom is the most recent event performed by a user.

    The
    > original text file is unsorted and I have that part licked (see below).
    >
    > Ann, any text, 1000
    > Ann, any text, 1023
    > Ann, any text, 1045
    > Bob, any text, 1000
    > Bob, any text, 1011
    > Bob, any text, 1100
    > Charlie, any text, 1001
    > Charlie, any text, 1118
    > Charlie, any text, 1124
    >
    > When I display my results I get what you see above. What I can't figure
    > out for the life of me (and it seems so simple) is how to compare all

    occurrences
    > of a name and display only the latest entry for each person (see below).
    >
    > Ann, any text, 1045
    > Bob, any text, 1100
    > Charlie, any text, 1124
    >
    > Do you see my dilemma? I am going grey!
    >
    > "Jim Burak" <jim.burak@cevp.com> wrote:
    > >
    > >Hello all,
    > >
    > >I have a question. I am writing something to sort a text file and

    display
    > >only the latest entry. Let me explain a little better. Take the

    following
    > >input file for example:
    > >
    > >Jim, any text1, 1000
    > >Bobby, any text2, 1001
    > >Sally, any text3, 1002
    > >Jim, any text4, 1214
    > >Bobby, any text5, 1222
    > >Sally, any text6, 1245
    > >
    > >I have written the code to sort the file by name and that works just

    fine.
    > > What's kicking my butt right now is displaying only the latest entry.

    > The
    > >third variable in each line is the time (a string) what I need the code

    > to
    > >do is after sorting in decending order is to find the two occurences of

    > say,
    > >Bobby, in the file and then compare the times and display the latest

    information
    > >or "any text5".
    > >
    > >To recap, I've got the sort down I just need a way to throw out all of

    the
    > >oldest times and display only the latest one.
    > >
    > >Any thoughts?
    > >
    > >Thanks in advance!
    > >
    > >JB
    > >
    > >Also could all responders also CC jim.burak@cevp.com in their replies?

    > I'll
    > >see that quicker than going to the website periodically.
    > >

    >




  3. #3
    Mark Andes Guest

    Re: Sorting a string file, comapring and displaying only the latest entries


    I can't email you (at work having lunch at the moment so hopefully you
    see this responce.

    Well I'm going to have to make a few assumptions based on what you have said:

    -You're loading these entries from a file in order to display only the most
    recent on a form.

    -You're loading the lines from the text file into a single string element
    of an array (seem logical and probably the simplest)

    -Your time field is always going to be four digits and/or you're using the
    commas as field delimiters

    In any case hopefully this will give you some direction (I'm making this
    as simple and generic as possible):

    ---code snip--------

    Public sub dsplyEntries(byref strLoaded() as string)
    Dim i as long 'incrementer
    Dim tempName as string 'Name in current entry
    Dim prevName as string 'Name of last entry used
    Dim tempTime as long 'Time of current entry
    'note: if the time isn't only numeric then you may need to change
    'some things
    Dim selLoaded as string 'Full string of the selected entry
    Dim selTime as long 'time of sleected entry

    'Setup the previous name string to prevent the loop from
    'immediately outputting the first entry
    i = lbound(strloaded)
    prevname = Left$(strLoaded(i), instr(strLoaded(i),",") - 1)

    'Loop through the array and select the most recent entries
    For i = lbound(strloaded) to ubound(strloaded)
    tempName = Left$(strLoaded(i), instr(strLoaded(i),",") - 1)
    tempTime = Val(Right(strLoaded(i), 4))

    if prevName <> tempName then
    'New name so output the selected entry
    text1.text = text1.text & " " & selLoaded
    'Reset selected items
    seltime = 0
    selLoaded = ""
    'input the next entry's information
    If i < ubound(strloaded) then
    prevname = Left$(strLoaded(i+1), instr(strLoaded(i+1),",") - 1)
    end if
    elseif tempTime > seltime then
    'basically if the new time is higher than the old
    'it selects this as the current entry
    selLoaded = strLoaded(i)
    selTime = tempTime
    end if
    next i

    End sub

    ---code snip---------

    I'm not as my dev system so I haven't really tested this (it's all off the
    top of my head) so this isn't the most efficient or elegant code I could
    make but I think it should get the job done and remain understandable. Let
    me know if I goofed on something

    Good luck
    Mark

    "Jim Burak" <jim.burak@cevp.com> wrote:
    >
    >Take the following text file for example:
    >
    >Ann, any text, 1000
    >Bob, any text, 1000
    >Charlie, any text, 1001
    >Bob, any text, 1011
    >Ann, any text, 1023
    >Ann, any text, 1045
    >Bob, any text, 1100
    >Charlie, any text, 1118
    >Charlie, any text, 1124
    >
    >I have an application that produces a file similar to this (see above).


    >The record at the bottom is the most recent event performed by a user.

    The
    >original text file is unsorted and I have that part licked (see below).
    >
    >Ann, any text, 1000
    >Ann, any text, 1023
    >Ann, any text, 1045
    >Bob, any text, 1000
    >Bob, any text, 1011
    >Bob, any text, 1100
    >Charlie, any text, 1001
    >Charlie, any text, 1118
    >Charlie, any text, 1124
    >
    > When I display my results I get what you see above. What I can't figure
    >out for the life of me (and it seems so simple) is how to compare all occurrences
    >of a name and display only the latest entry for each person (see below).
    >
    >Ann, any text, 1045
    >Bob, any text, 1100
    >Charlie, any text, 1124
    >
    >Do you see my dilemma? I am going grey!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center