Click to See Complete Forum and Search --> : Sorting array problem :(


tonyeck
12-04-2004, 11:52 PM
Hey all :)

I have a small sorting problem within Javascript and 2D arrays. Basically i have created an array like so inside a function which calculates the final out put:



function go(){

var table1= [
["Kestrel Moon", 1165, 475, 0, 97, 0, 98],
["BB", 1231, 380, 89, 43, 180, 85],
["Whatever Your Drive", 1052, 210, 34, 23, 450, 43],
["Discount Drivers", 1667, 310, 291, 302, 189, 93],
["Indirect Curve", 804, 599, 0, 45, 0, 0],
["Swindel", 1010, 210, 187, 35, 32, 103],
["Captain", 510, 100, 0, 19, 0, 40],
["Saintly Drivers", 890, 0, 0, 0, 0, 155],
["Ipswich Union", 490, 0, 0, 0, 0, 0],
["Young Drivers", 803, 0, 203, 78, 0, 82],
["Atlee", 923, 200, 107, 81, 92, 70],
["Good Deal", 1690, 300, 434, 112, 493, 47],
["Nice Price", 775, 0, 0, 487, 0, 30],
["Insure for Less", 1435, 0, 998, 49, 0, 59],
["Instant Insure", 1223, 424, 43, 113, 68, 96]
];


The whole point of this for me is to learn javascript functions, calculations and sorting these. So the arrays basically are values for discounts from the insurance company (i.e. kestrel moon) and you are given discount say if you were above a certain age:

for (var i=0;i<table1.length;i++)
{



//______________________Defining the total cost

total_cost = table1[i][1];



//______________________If statments
//______________________If age is less than 25 - discount!
if (age > 25)
{
total_cost = total_cost - table1[i][2];
parent.frames.bottom.document.write("Over 25 years old: £"+table1[i][2]+"<BR>")
}


I have a whole bunch of If statments working out the discounts etc. Then from a form (where the user inputs data like age etc) a function is run (where all the if statements etc are kept). The results are then writen to a new frame. I want, from the output, to be able to show the user what the best price would be. The "total_cost" is worked out after all the discounts have been subtracted and was wondering how to pick out the lowest result?

This is the code which writes (and loops to write for each company) the final cost:

parent.frames.right.document.write("<table border='6'><td width='150'>"+table1[i][0]+"</td><td width='150'>Offers you: £"+total_cost+"</td></tr>")


This has been bugging me for so long! I just cant seem to work out how I would be able to pull out the lowest cost and display this... Can anyone help? Sorry its not explained very well, but it might help if u see the site and the source code....

Here is the document at the moment (not finished design wise):

http://homepage.mac.com/tonyeck/new/
http://homepage.mac.com/tonyeck/new/left.html (for source of left frame if browser cant take it from the index)


Thanks guys

Kor
12-05-2004, 05:25 AM
build an total cost as an array, rise increment by 1 each time when running the function. Sort this array (myarray.sort()). Choose first element.

Vladdy
12-05-2004, 11:00 AM
You data can be better described by and array of objects. Then writing the sorting function becomes more intuitive...

7stud
12-05-2004, 11:01 AM
Hi,

It doesn't look like there is any reason to use sort(). Picking out the lowest(or highest) cost can be done more efficiently than sorting the whole array. The basic idea is that you grab the cost of the first element in the array, and then move along the array and compare the cost of the subsequent elements to the first element. If a subsequent element has a lower cost, then you store that cost and position in the array as your new lowest cost element. Subsequent array elements are compared to the new low cost, and when you get to the end of the array, you will have your lowest cost.

The following is an example on a 1d array. Picking out the lowest cost for your array is only slightly more complicated because picking out the cost requires two subscripts.


var table1= [10, 2, 4, 40, 5, 1, 6]

//initialize the vars with the first array element:
var lowest_cost_index = 0;
var lowest_cost = table1[0];

for(var i = 1; i < table1.length; i++)
{
if (table1[i] < lowest_cost)
{
lowest_cost = table1[i];
lowest_cost_index = i;
}
}

alert(lowest_cost);
alert(lowest_cost_index);

With your array, the lowest cost index position will allow you to access any of the values in the subarray at that index position.

tonyeck
12-05-2004, 03:49 PM
Originally posted by Vladdy
You data can be better described by and array of objects. Then writing the sorting function becomes more intuitive...

Thats everyone for you help, its been much appreciated! :)

Vladdy, thanks for the example. I am very new to javascript and ur example helped me understand what I need to do, yet I still cant seem to sort it yet :( Not sure how to adopt that method for a 2d array in my situation.....

How does the lowest cost index relate to the lowest total cost which is worked out?

7stud
12-05-2004, 04:53 PM
Arrays work like this:

table1
values: 10 2 4 40 5 1 6
index position: 0 1 2 3 4 5 6



var table1 = [10, 2, 4, 40, 5, 1, 6];
alert(table1[0]);

The low_cost_index tells you where in the array the low cost element was found. In your array, there is other information at the low_cost_index position besides the cost because every element in your array is an array itself:

index position 0: ["Kestrel Moon", 1165, 475, 0, 97, 0, 98],
index position 1: ["BB", 1231, 380, 89, 43, 180, 85],
index position 2: ["Whatever Your Drive", 1052, 210, 34, 23, 450, 43],

For instance, if you wanted to display the name of the lowest cost plan to the user, you could do this:

alert(table1[low_cost_index][0]);

tonyeck
12-05-2004, 07:47 PM
I think you have lost me slightly, sorry mate :(

I think I get what you mean regarding the 2d array, but loose me trying to find the lowest cost.

I am confused what code I should write, as I am trying to display the lowest cost after all deductions have been made. So I would have thought it may have been something like:

var lowest_cost_index = 0;
var lowest_cost = table1[1] - total_cost;


for(var i = 1; i < table1.length; i++)
{
if (table1[i] < lowest_cost)
{
lowest_cost = table1[i] - total_cost;
lowest_cost_index = i;

}
}
alert(lowest_cost);
alert(lowest_cost_index);



I am aware this wont work, however, If I just copy the code you gave me before, it "alerts" the lowest alphabetical ordered element...

At the end I want, "Your lowest quote is from "company name" - (insert lowest total_cost here."

(thanks for the continued help though!)

7stud
12-05-2004, 09:43 PM
var lowest_cost = table1[1] - total_cost;

1)In your array what is table1[1]?

2)In that line of code, what does total_cost equal?

tonyeck
12-06-2004, 08:42 AM
Originally posted by 7stud
var lowest_cost = table1[1] - total_cost;

1)In your array what is table1[1]?

2)In that line of code, what does total_cost equal?


Sorry mate but I scrapped it now as it wasn't working. I have almost got it now i think! Using this method, I am able to show the lowest total_cost:


//Setting variables
var lowest = 5000;
var lowcompany = 0;

//In the for loop

total_cost = table1[i][1];

if (total_cost <lowest) {
lowest=total_cost;

}

//After the loop

parent.frames.right.document.write(lowest)


However I am unsure how to get it to name which company relates to this total cost :(

7stud
12-06-2004, 12:05 PM
From my earlier post:
The low_cost_index tells you where in the array the low cost element was found. In your array, there is other information at the low_cost_index position besides the cost because every element in your array is an array itself:

index position 0: ["Kestrel Moon", 1165, 475, 0, 97, 0, 98],
index position 1: ["BB", 1231, 380, 89, 43, 180, 85],
index position 2: ["Whatever Your Drive", 1052, 210, 34, 23, 450, 43],

For instance, if you wanted to display the name of the lowest cost plan to the user, you could do this:

alert(table1[low_cost_index][0]);

In your array, I believe this array has the lowest number at index position 1:

["Ipswich Union", 490, 0, 0, 0, 0, 0];

Your 2d array is an 'outer' array that encompasses many inner arrays similar to that one. The for-loop looks at one inner array at a time, and then moves on to the next inner array. When you find the inner array with the lowest cost, if you save the location of the inner array's position in the outer array, then you can access any of the information in the inner array. And, as luck would have it, 'i' indicates the location of each inner array. So, when you find the inner array containing the lowest cost, if you save it's outer array index position, which again is 'i', then you can use 'i' to access any of the information in the inner array.

Here are some exercises you need to figure out:

1) What is the reference to the whole array that has "Whatever Your Drive" as it's first element, e.g. if you wanted to alert() the whole array, how would you do that using only one alert() statement and no loops?

2) If you are told the index position of the inner array with the lowest cost is 8 in the outer array, how could you alert() just the name?