JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old August 21st, 2006, 04:40 PM
prioritypie prioritypie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 90 prioritypie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 9 m 20 sec
Reputation Power: 4
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
} 

Reply With Quote
  #2  
Old August 22nd, 2006, 12:09 PM
vbrtrmn's Avatar
vbrtrmn vbrtrmn is offline
4:04 Time Not Found
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2004
Location: Northern Virginia
Posts: 1,885 vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level)vbrtrmn User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 16 h 9 m 10 sec
Reputation Power: 177
Send a message via ICQ to vbrtrmn Send a message via AIM to vbrtrmn Send a message via MSN to vbrtrmn Send a message via Yahoo to vbrtrmn
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.

Reply With Quote
  #3  
Old August 23rd, 2006, 02:08 PM
prioritypie prioritypie is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 90 prioritypie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 9 m 20 sec
Reputation Power: 4
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Sorting array of objects


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway