How to order array results??-VBForums

# Thread: How to order array results??

1. ## 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. ## Re: How to order array results??

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

3. ## 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?

4. ## 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. ## 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. ## 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. ## 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. ## 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.

Originally Posted by TheKian
... 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```

9. ## Re: How to order array results??

Originally Posted by passel
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

10. ## 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:
`Public Class Car    Public Property Index As Integer    Public Property Make As String    Public Property Model As String    Public Property Year As Integer    Public Property GasMileage As Integer     Public Overrides Function ToString() As String        Return String.Format("#{0:00} - {1} - {2} {3} - MPG: {4}", Me.Index, Me.Year, Me.Make, Me.Model, Me.GasMileage)    End Function End Class Public Class MileageSorter    Implements IComparer(Of Car)     Public Function Compare(x As Car, y As Car) As Integer Implements IComparer(Of Car).Compare        If x.GasMileage = y.GasMileage Then            Return 0        ElseIf x.GasMileage < y.GasMileage Then            Return -1        ElseIf x.GasMileage > y.GasMileage Then            Return 1        Else            Return 0        End If    End FunctionEnd Class`

on the sample form, one button, two listboxes:
vb.net Code:
`Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Dim carLot As New List(Of Car)        Dim gm As New Random()         For a = 1 To 10            Dim c As New Car With {.Index = a, .Make = "Ford", .Model = "F150", .Year = 2000 + a, .GasMileage = gm.Next(10, 50)}            carLot.Add(c)        Next         For Each c In carLot            ListBox1.Items.Add(c.ToString)        Next         carLot.Sort(New MileageSorter)         For Each c In carLot            ListBox2.Items.Add(c.ToString)        Next      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.

-tg

#### 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