|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Sorting array of objects
I'm trying to sort an array which is made up of a number of custom objects.
I define an empty array, and then populate it with objects with parameters coming from some form fields. I then try and use a custom sort function to sort the array based on one of the parameters. Whatever I've done so far just ends up with array of [object Object], [object Object]... (that's exactly what an alert of my array says). Here is the code: Code:
//This is just an array with some things that are going to be parameters of the object
var statements=new Array("Be my own boss","Make my own decisions","Be responsible for my future","Improved work life balance","Earn more money","Use my skills effectively","Develop my skills further","Do something I really enjoy");
//Define the array to hold all the objects
var records = new Array();
function sortByImp(a, b) { //Sorting function - there must be something wrong here
var x = a.Importance;
var y = b.Importance;
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
}
function record(state, imp, pos, neg) { //The "record" object constructor
this.Statement = state;
this.Importance = parseInt(parseFloat(imp));
this.Postv = pos;
this.Negtv = neg;
}
function addToArray() { //Populate the array with new objects
for (i=1; i<9; i++)
{
if (document.frm2_1_2['q2_1_2_'+i].value == 1)
{
var statemnt = statements[i];
var imps = document.frm2_1_2['q2_1_2_'+i+'_2'].value;
var poss = document.frm2_1_2['q2_1_2_'+i+'_3'].value;
var negs = document.frm2_1_2['q2_1_2_'+i+'_4'].value;
records[records.length++] = new record(statemnt, imps, poss, negs); //Create a new record object with the values from the form and add the object into the records array
}
}
return records;
}
function doWrite() { //The function where I want to use the array of records
var answers = addToArray();
answers.sort(sortByImp);
//Then some more stuff here that uses the array
}
|
|
#2
|
||||
|
||||
|
Dump the data out of your objects..
Code:
for (var i=0;i<records.length;i++) {
for (var k in records[i]) {
alert('records['+ i +'].'+ k +' = '+ records[i][k]);
}
}
*edit forgot to close my code tag .. ![]()
__________________
------------- vbrtrmn -------------- i think i'm missing some vowels here ------------------------------------ ---------- js.antinoc.net ---------- ------------------------------------ --- The Two Types of Programmers --- Motorcycles are cooler than computers! Last edited by vbrtrmn : August 22nd, 2006 at 12:22 PM. |
|
#3
|
|||
|
|||
|
ok...
Hey, thanks for the response.
Trouble is, I can't quite work out what to do with it! When I put the code you wrote in the "addToArray" function, sure enough it spurted out the first value that should be in the array. But what I still can't achieve is the array being passed back to where I want to use it. I tried modifying your alert so that my addToArray function became: Code:
function addToArray() { //Populate the array with new objects
for (i=1; i<9; i++)
{
if (document.frm2_1_2['q2_1_2_'+i].value == 1)
{
var statemnt = statements[i];
var imps = document.frm2_1_2['q2_1_2_'+i+'_2'].value;
var poss = document.frm2_1_2['q2_1_2_'+i+'_3'].value;
var negs = document.frm2_1_2['q2_1_2_'+i+'_4'].value;
records[records.length++] = new record(statemnt, imps, poss, negs); //Create a new record object with the values from the form and add the object into the records array
}
}
for (var i=0;i<records.length;i++) {
for (var k in records[i]) {
records[i].k = records[i][k];
}
}
return records;
}
I think the problem is that I don't really understand what you meant by "dump your values"... Thanks for any help. |
![]() |
| Viewing: Dev Shed Forums > Web Design > JavaScript Development > Sorting array of objects |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|