Comparing multiple numbers

1. Junior Member
Join Date
Dec 1969
Posts
6

## Comparing multiple numbers

How can I compare multiple numbers and choose the smallest number? For example, if a person buys 3 books they get the fourth one free. I need to compare prices of selected books and return the lowest price (the lowest priced book is free...)<BR><BR>i.e. 4 books are selected, priced at 17.95, 18.95, 15.95, 16.95. The 15.95 book is the cheapest and therefor free. Mathmatically and logically how do I compare the prices and return the cheapest?<BR><BR>These numbers are coming from text fields in a form.<BR><BR>Hope that makes sense. Any help is greatly appreciated.

2. Senior Member
Join Date
Dec 1969
Posts
1,274

## RE: Comparing multiple numbers

The cheap and dirty way is to assign all four values to four different variables, like:<BR><BR>foo1=17.95<BR>foo2=18.95<BR>foo3=15.9 5<BR>foo4=15.95<BR><BR>Then for each variable, compare it to all the others:<BR>If foo1&#060;foo2 then<BR> foo1container = foocontainer + 1<BR>else<BR> foo1container = foocontainer + 0<BR>end if<BR>If foo1&#060;foo3 then<BR>...<BR>If foo2&#060;foo1 then<BR>...<BR><BR>You get the idea? Then, when you&#039;re done, whichever variable has the value of &#039;3&#039; will have been the lowest one, and therefore free.<BR><BR>Make sense?

3. Senior Member
Join Date
Dec 1969
Posts
129

## RE: Comparing multiple numbers

The prices are probably not coming from a form , they are coming from a database. Write your SQL statement to ORDER BY price ASC. then to give the first record for free. Subtract the first record price from the total.

4. Junior Member
Join Date
Dec 1969
Posts
6

## RE: Comparing multiple numbers

Thanks for the reply, but the price is a variable associated with the requested form field, not coming from a database. I am submitting to a database not pulling from a database, but price is not a field. The sku is a field, and the number of books selected from each sku is multiplied by the price, so I don&#039;t think an SQL statement is what I am looking for.

5. Senior Member
Join Date
Dec 1969
Posts
1,274

## Did you read my post?

.

6. Senior Member
Join Date
Dec 1969
Posts
500

## -----TRY THIS-----

Name all your textboxes with the same name, say "bookprice".<BR>Then do:<BR><BR>BookPrices = Split(request.form("bookprice"), ",")<BR>iMax = uBound(BookPrices)<BR>For x = 0 to iMax<BR>Â*Â*BookPrices(x) = Trim(BookPrices(x))<BR>Next<BR><BR>SortArray(BookP rices)<BR><BR>BookPrices(0) = 0<BR>Total_Price = 0<BR>For x = 0 to iMax<BR>Â*Â*Total_Price = Total_Price + CSNG(BookPrices(x))<BR>Next<BR><BR><BR>Sub SortArray(aTempArray) <BR> Dim iTemp, jTemp, strTemp<BR><BR> For iTemp = 0 To UBound(aTempArray) <BR> For jTemp = 0 To iTemp <BR><BR> If strComp(aTempArray(jTemp),aTempArray(iTemp)) &#062; 0 Then<BR> &#039;Swap the array positions<BR> strTemp = aTempArray(jTemp) <BR> aTempArray(jTemp) = aTempArray(iTemp) <BR> aTempArray(iTemp) = strTemp <BR> End If <BR><BR> Next <BR> Next <BR>End Sub<BR>

7. Senior Member
Join Date
Dec 1969
Posts
1,274

## Eh?

I dropped in the values \$19.95,\$18.95,\$21.95,\$17.95 in that order, and the code returned a Total_Price of \$58.85, which was the first value. It didn&#039;t sort properly, it merely dropped the first item, which is not the point of the exercise. The total should have been \$60.85.

8. Senior Member
Join Date
Dec 1969
Posts
500

## Oh. How Stupid.

I didn&#039;t test it, I just found this sort routine, and stuck it in there, and slapped it up here.<BR><BR>Mistake.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•