Results 1 to 12 of 12

Thread: Creating an index for sorting....*solved :-) *

  1. #1

    Thread Starter
    Lively Member Maldini's Avatar
    Join Date
    Sep 2001
    Posts
    73

    Creating an index for sorting....*solved :-) *

    How can I create a procedure that creates an index which can order a series of records by a specific field?

    For example, I hv four fields, Name, DOB, Gender and Weight saved in a random access file.... how can I make it so that the procedure orders by weight?

    Does this involve making another array or file?

    Would some1 mind giving some coded example of something along this lines?

    Thx.
    Last edited by Maldini; Nov 6th, 2001 at 11:05 AM.

  2. #2
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    How will you be displaying you records anyway? If in a MSFLEXGRID, this control has it's own built-in sorting mechanism...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  3. #3

    Thread Starter
    Lively Member Maldini's Avatar
    Join Date
    Sep 2001
    Posts
    73
    I'm going to have to display these in either a listbox or a listview control...

  4. #4
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Listbox would be a pian, but listview , I belive has sort option. I'll SEARCH...
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  5. #5
    PowerPoster
    Join Date
    Aug 2000
    Location
    IN SILENCE
    Posts
    6,441

    Well

    Here's a thread to help you out...

    http://www.vbforums.com/showthread.p...hreadid=111987
    Remaining quiet down here !!!

    BRAD HAS GIVEN ME THE ULTIMATIVE. I have chosen to stay....

  6. #6

    Thread Starter
    Lively Member Maldini's Avatar
    Join Date
    Sep 2001
    Posts
    73
    Thx....

    for some reason, the teacher doesn't want listviews all uv a sudden, so is there a way to do this with a listbox?

    like the task wants "a precedure that creates an index such that the index would sort the file by weight"....

    and this is supposed to havta do with using an array as the index, and the index is somehow supposed to b a serial file itself

  7. #7

    Thread Starter
    Lively Member Maldini's Avatar
    Join Date
    Sep 2001
    Posts
    73
    plz???

  8. #8
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527

    Re: Well

    Originally posted by James Stanich
    Listbox would be a pian, but listview , I belive has sort option. I'll SEARCH...
    so does listbox...

  9. #9

    Thread Starter
    Lively Member Maldini's Avatar
    Join Date
    Sep 2001
    Posts
    73
    it does hv the option?
    can u show me how to use it for my purpose?

  10. #10

    Thread Starter
    Lively Member Maldini's Avatar
    Join Date
    Sep 2001
    Posts
    73
    plz help me....

  11. #11
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    Here is some code for u to play around with
    Regards
    Stuart

    VB Code:
    1. 'FORM CONTAINS
    2. 'COMMAND1 TO CREATE FILE
    3. 'COMMAND2 TO READ FILE
    4. 'COMBO1 TO CHOOSE METHOD OF SORTING . .STYLE = 2
    5. 'LIST1 RESULTS... SORTED = FALSE, VISIBLE = TRUE
    6. 'LIST2 SORTING ... SORTED = TRUE, VISIBLE = FALSE
    7. 'u can leave 2 visible to see what is happening
    8. Option Explicit
    9.  
    10. Private Type PersonType
    11.     Name As String * 40
    12.     DOB As String * 8 'yyyymmdd format for sorting
    13.     Gender As String * 1
    14.     Weight As Single
    15. End Type
    16. Dim Person() As PersonType
    17. Dim lCounter As Integer
    18. Dim lFileNum As Byte
    19.  
    20. Private Sub Form_Load()
    21.     'Fill combo with sorting methods
    22.     With Combo1
    23.         .AddItem "Sort by name"
    24.         .AddItem "Sort by date"
    25.         .AddItem "Sort by gender"
    26.         .AddItem "Sort by weight"
    27.         .Enabled = False
    28.     End With
    29.  
    30. End Sub
    31.  
    32. Private Sub Command1_Click()
    33.    
    34.     'Make a dummy file
    35.     ReDim Person(4)
    36.     With Person(0): .Name = "Smith, John": .DOB = "19710320": .Gender = "M": .Weight = 72: End With
    37.     With Person(1): .Name = "Jones, Jenny": .DOB = "19680422": .Gender = "F": .Weight = 64: End With
    38.     With Person(2): .Name = "Bloggs, Bob": .DOB = "19851201": .Gender = "M": .Weight = 80: End With
    39.     With Person(3): .Name = "Green, Theresa": .DOB = "19780411": .Gender = "F": .Weight = 55: End With
    40.     With Person(4): .Name = "Brown, David": .DOB = "19910708": .Gender = "M": .Weight = 68: End With
    41.    
    42.     lFileNum = FreeFile 'Find file number
    43.     Open "C:\Sample.txt" For Random As lFileNum Len = Len(Person(0))
    44.     For lCounter = 0 To UBound(Person)
    45.         Put lFileNum, lCounter + 1, Person(lCounter) 'Write to file
    46.     Next
    47.     Close lFileNum
    48.     Command1.Enabled = False
    49. End Sub
    50.  
    51.  
    52. Private Sub Command2_Click()
    53.     Dim lLength As Long
    54.    
    55.     lLength = FileLen("c:\Sample.txt") \ Len(Person(0)) 'find no of records
    56.     ReDim Person(lLength) 'Make array for number of records
    57.     'Read file .. assume file was made at any time
    58.     lFileNum = FreeFile 'Find file number
    59.     Open "C:\Sample.txt" For Random As lFileNum Len = Len(Person(0))
    60.     For lCounter = 0 To UBound(Person)
    61.         Get lFileNum, lCounter + 1, Person(lCounter)
    62.     Next
    63.     Close lFileNum
    64.     Command2.Enabled = False
    65.    
    66.     With Combo1
    67.         .Enabled = True
    68.         .ListIndex = 0 'Default to sort by name
    69.     End With
    70. End Sub
    71.  
    72. Private Sub Combo1_Click() 'Style 2 list only
    73.     'Sort based on selection
    74.     With List2 'List 2 is sorted and invisible
    75.         .Clear
    76.         For lCounter = 0 To UBound(Person)
    77.             Select Case Combo1.ListIndex
    78.                 Case 0: .AddItem Person(lCounter).Name
    79.                 Case 1: .AddItem Person(lCounter).DOB
    80.                 Case 2: .AddItem Person(lCounter).Gender
    81.                 Case 3: .AddItem Format$(Person(lCounter).Weight, "0000.0")
    82.             End Select
    83.             'store the record number in the itemdata
    84.             .ItemData(.NewIndex) = lCounter
    85.         Next
    86.     End With
    87.    
    88.     'Transfer sorted data to list1
    89.     List1.Clear
    90.     For lCounter = 0 To List2.ListCount - 1
    91.         With Person(List2.ItemData(lCounter))
    92.             'Copy list2 to list 1 in same order
    93.             List1.AddItem Trim$(.Name) & vbTab & .DOB & vbTab & .Gender & vbTab & .Weight
    94.         End With
    95.     Next
    96.     List1.ListIndex = 0
    97. End Sub
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  12. #12

    Thread Starter
    Lively Member Maldini's Avatar
    Join Date
    Sep 2001
    Posts
    73
    Thx man, the code works brill

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