How to order array results??-VBForums
Results 1 to 10 of 10

Thread: How to order array results??

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    How to order array results??

    So this app takes in prices of fuels and miles per gallons to find how much each car costs per annum.

    Because I want to calculate more than one car at once, I used an array for the cars called Car(10).
    The way I have done this is in a loop that keeps going as long as "i" is less than 11 and i = i+1 after each loop.

    Each result is under Car(i). How would I order the cars in order of price displayed from their numbers (they don't have names) and displaying the prices also.

  2. #2
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: How to order array results??

    Hi. Can you please post what you have tried so far.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: How to order array results??

    The full code is herehttp://pastebin.com/KHihnGXA

    But here are the main bits: http://pastebin.com/j5EaCMff

    Is this what you want?
    Last edited by TheKian; Jul 27th, 2014 at 03:41 PM. Reason: Grammar

  4. #4
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    4,948

    Re: How to order array results??

    Your question is ordering an array list. Not looking through your whole project. Please only post the parts relevant to the question asked. We are all volunteers. We don't have time to look at every question asked solutions.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: How to order array results??

    Sorry, I wasn't sure exactly what you wanted so I just tried to get the main bits in.
    The goal wasn't to waste any time hence the second link but I'm sorry if it wasn't precise enough. What precisely do I need to give you?
    Thanks

  6. #6
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,923

    Re: How to order array results??

    Well, it looks like you need to keep track of the index of the cars, when you sort.
    You only have a single variable that you are trying to use to track two things,
    It looks like you are using index + 1 as the car number and setting car(i) to that in this line
    VehicleAdding: Car(i) = i + 1

    but later set Car(i) to the computed value, and you have different versions of that.
    Car(i) = POP * LPA 'PricePerAnnum (in pence) = PriceOfFuel x LitresUsedPerYear

    So, one way you can sort the values and keep track of the index, is to use put the indexes in an array and move them around in order based on the values the indexes point to.
    Ident is excellent at lambda and LinQ, whereas I am not, so there are probably several ways to improve this basic example, but this is a quick off the cuff example where you sort the indexes to order the array, thus you keep the index numbers (car number) and the values associated with them, so you can use them, as is printed out in the last loop.
    Code:
    Public Class Form1
    
      Dim cars() As Single = {4.23, 7.23, 2.58, 5.67, 3.28, 9.12, 7.25, 8.15, 4.36}
    
      Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        'An index array to keep track of car number to car value association
        'Initialize the array
        Dim idx(UBound(cars)) As Integer  'I should learn linq/lambda to make some of these tasks simpler
        For i As Integer = 0 To idx.Count - 1
          idx(i) = i
        Next
    
        'simple, unoptimized bubble sort  (sorts the index (car number) based on car value the index points to)
        For i = UBound(idx) To 1 Step -1
          For j = 0 To i - 1
            If cars(idx(j)) > cars(idx(j + 1)) Then   'if this car's value is greater than the one following
              idx(j) = idx(j) Xor idx(j + 1)          'swap the two indexes (using 3 xor operations)
              idx(j + 1) = idx(j) Xor idx(j + 1)
              idx(j) = idx(j) Xor idx(j + 1)
            End If
          Next
        Next
    
        'Display order in the output window of the IDE (sometimes in the Immediate window if that option is selected)
        For i As Integer = 0 To UBound(idx)
          Debug.Print(String.Format("Car {0}, {1}", idx(i), cars(idx(i))))
        Next
    
      End Sub
    End Class

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2013
    Posts
    8

    Re: How to order array results??

    I'm pretty much a massive noob so excuse me for not understanding but you've made cars a "single" variable and given them certain values. What are these values for and what do they mean. If it's supposed to be costs per year for each car how to I change it so each value is one that has been previously calculated. Thanks

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    3,923

    Re: How to order array results??

    I don't know. I was trying to interpret your code.
    Perhaps your code was pseudo code and didn't actually spell out what cars(i) was, or mean.
    But in the code you linked to, you had the cars() array declared as a Decimal type, which means each element of the array can hold a single value.
    That is why the code makes no sense because you set it to two different things in different parts of the code.

    As far as my code example goes, it was just an example I wrote earlier, before you posted your code. Since Ident had posed a question by the time I had it written, I waited to see what your response would be before I posted it.

    I didn't make cars a single value, I made it an array and assigned nine values to it so the example code would have something to sort, and assuming the order of the array as set is the order of the cars by number (which your "cars(i) = i+1" would seem to indicate), the example shows how to create an "index" array, which serves both as the car number (the index value), and the sorted array (you sort the indexes by the value of what they point to).
    This gives you an array you can loop through in order from 0 to whatever, and find which car is at that spot (the value at that item of the array), and the value associated with that car (using the value from the index array to index into the cars array).

    This was an effort to work with what you have, but in reality, you should probably have a car class that holds all the things you need to know about a car, and have an list of instances of that class.

    I gave it a shot, but if you are a massive noob then perhaps the task you've jumped into is a bit more involved than you should be dealing with right now. I have limited time, so can possibly answer a specific question on a particular point, but I definitely don't have the time to be a tutor about VB.Net in general.
    Some have taking on that task and provide various tutorials, but I'll probably have to let this go as I am a bit swamped at the moment.

    Quote Originally Posted by TheKian View Post
    ... If it's supposed to be costs per year for each car how to I change it so each value is one that has been previously calculated. Thanks
    Just remove my declaration for cars(i), use your code to populate the array, then either use the code in the button click to process the array, or put that code in a sub that you can call. It is just an example. It is up to you to understand what it is doing then pull what you need from the example and tailor it to your situation.

    You can see the order of the values in the source code
    {4.23, 7.23, 2.58, 5.67, 3.28, 9.12, 7.25, 8.15, 4.36}
    So cars(0) is 4.23 and cars(1) is 7.23, etc...
    If you run the code you can see the sorted output, note below that car 0 is still 4.23 and car 1 is 7.23.
    Code:
    Car 2, 2.58
    Car 4, 3.28
    Car 0, 4.23
    Car 8, 4.36
    Car 3, 5.67
    Car 1, 7.23
    Car 6, 7.25
    Car 7, 8.15
    Car 5, 9.12
    Last edited by passel; Jul 28th, 2014 at 10:41 AM.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,107

    Re: How to order array results??

    Quote Originally Posted by passel View Post
    in reality, you should probably have a car class that holds all the things you need to know about a car, and have an list of instances of that class.
    That's what I was thinking... create a class to hold all the car information... create a list (Of Car) ... then create implement the IComparer interface (which is really easy to do) to perform the sorting...

    let me see if I can whip something up.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,107

    Re: How to order array results??

    some sample code....


    First the car class... the .ToString override was so that the sample code that adds it to the list box (for display) will show what I want.
    the key though is the MileageSorter class... it implements the ICompare (Of T) interface... it compares the Mileage, and returns -1 if x is less than y, 1 if y is greater and 0 if they are equal.
    vb.net Code:
    1. Public Class Car
    2.     Public Property Index As Integer
    3.     Public Property Make As String
    4.     Public Property Model As String
    5.     Public Property Year As Integer
    6.     Public Property GasMileage As Integer
    7.  
    8.     Public Overrides Function ToString() As String
    9.         Return String.Format("#{0:00} - {1} - {2} {3} - MPG: {4}", Me.Index, Me.Year, Me.Make, Me.Model, Me.GasMileage)
    10.     End Function
    11.  
    12. End Class
    13.  
    14. Public Class MileageSorter
    15.     Implements IComparer(Of Car)
    16.  
    17.     Public Function Compare(x As Car, y As Car) As Integer Implements IComparer(Of Car).Compare
    18.         If x.GasMileage = y.GasMileage Then
    19.             Return 0
    20.         ElseIf x.GasMileage < y.GasMileage Then
    21.             Return -1
    22.         ElseIf x.GasMileage > y.GasMileage Then
    23.             Return 1
    24.         Else
    25.             Return 0
    26.         End If
    27.     End Function
    28. End Class

    on the sample form, one button, two listboxes:
    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.         Dim carLot As New List(Of Car)
    3.         Dim gm As New Random()
    4.  
    5.         For a = 1 To 10
    6.             Dim c As New Car With {.Index = a, .Make = "Ford", .Model = "F150", .Year = 2000 + a, .GasMileage = gm.Next(10, 50)}
    7.             carLot.Add(c)
    8.         Next
    9.  
    10.         For Each c In carLot
    11.             ListBox1.Items.Add(c.ToString)
    12.         Next
    13.  
    14.         carLot.Sort(New MileageSorter)
    15.  
    16.         For Each c In carLot
    17.             ListBox2.Items.Add(c.ToString)
    18.         Next
    19.  
    20.  
    21.     End Sub

    Here's a shot of the results... you can see the first list has them in order added, the second in order by Mileage.

    Name:  VBF-SampleSort.JPG
Views: 80
Size:  37.7 KB

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.