473,323 Members | 1,570 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

Secondary sort

Hi,

I have an array of objects. My object definition is given below:

function tempArray(code,height,weight)
{
this.code = code;
this.height = height;
this.weight = weight;
}
I used the following function to sort.

function sortBy(prop,arr) {
sortProp=prop;
arr=arr.sort(sortFunc);
}
function sortFunc(part1,part2) {
if (part1[sortProp]>part2[sortProp]) retVal=-1;
else if (part1[sortProp]<part2[sortProp]) retVal=1;
else retVal=0;
return retVal;
}

This however allows me to sort only by one criteria. I need to sort by
weight first and then by height for those items that have the same
weight. Any help in the form of algorithms or code snippets will be
highly appreciated.

Thanks in Advance.
Jul 20 '05 #1
4 3262
ei************@hotmail.com (Seeker) writes:
I used the following function to sort.

function sortBy(prop,arr) {
sortProp=prop;
arr=arr.sort(sortFunc);
}
You use a global variable to keep the property to sort by. Instead you
could use a local sortFunc function that can see the "prop" variable
directly. That is safer in a multithreaded environment.
This however allows me to sort only by one criteria. I need to sort by
weight first and then by height for those items that have the same
weight. Any help in the form of algorithms or code snippets will be
highly appreciated.


If you want to be generic:

Array.prototype.sortByProperties = function sortByProperties() {
var propArr = arguments; // not sure IE 4 understands. NS4 does.
this.sort(function(a,b) {
for(var i=0;i<propArr.length;i++) {
var ap = a[propArr[i]], bp = b[propArr[i]];
if (ap < bp) {return -1;}
if (ap > bp) {return 1;}
}
return 0;
})
return this;
};

You can then use it to sort by the properties you want:

arr.sortByProperties("weight","height");

or even

arr.sortByProperties("weight","height","code");

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2
Hi,
Thank you for the prompt response. I pasted the code and called it as
given below:
tempArrays.sortByProperties("weight","height");

I am not able to get the code to work. I keep getting an "undefined is
null or not an object" error. Also, I need for the code to work in IE.
We don't support netscape. Kindly help me.

Thanks once again.

Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<ad**********@hotpop.com>...
ei************@hotmail.com (Seeker) writes:
I used the following function to sort.

function sortBy(prop,arr) {
sortProp=prop;
arr=arr.sort(sortFunc);
}


You use a global variable to keep the property to sort by. Instead you
could use a local sortFunc function that can see the "prop" variable
directly. That is safer in a multithreaded environment.
This however allows me to sort only by one criteria. I need to sort by
weight first and then by height for those items that have the same
weight. Any help in the form of algorithms or code snippets will be
highly appreciated.


If you want to be generic:

Array.prototype.sortByProperties = function sortByProperties() {
var propArr = arguments; // not sure IE 4 understands. NS4 does.
this.sort(function(a,b) {
for(var i=0;i<propArr.length;i++) {
var ap = a[propArr[i]], bp = b[propArr[i]];
if (ap < bp) {return -1;}
if (ap > bp) {return 1;}
}
return 0;
})
return this;
};

You can then use it to sort by the properties you want:

arr.sortByProperties("weight","height");

or even

arr.sortByProperties("weight","height","code");

/L

Jul 20 '05 #3
ei************@hotmail.com (Seeker) writes:
Thank you for the prompt response. I pasted the code and called it as
given below:
tempArrays.sortByProperties("weight","height");


I tried it in IE6 with just:

var testArray = [{x:4,y:20},{x:10,y:15},{x:7,y:12},{x:4,y:15},{x:10 ,y:13}];
testArray.sortByProperties("x","y");

The testArray is sorted correctly. You have to show more code for us
to find the bug :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #4
Hi,

I obviously did something wrong. Thank you for your patience though. I
however found that the following code snippet works.

//************************************************** ************
//* This function sorts an array by weight (heaviest first) *
//************************************************** ************
function sortByWeight(a, b) {
a = a.weight;
b = b.weight;
return ((a > b) ? -1 : ((a < b) ? 1 : sortByHeight(a, b)));
}

//************************************************** ************
//* This function sorts an array by height (shortest first) *
//************************************************** ************
function sortByHeight(a, b) {
a = a.height;
b = b.height;
return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}

tempArrays.sort(sortByWeight);
Hope this helps others.
ei************@hotmail.com (Seeker) wrote in message news:<fb**************************@posting.google. com>...
Hi,
Thank you for the prompt response. I pasted the code and called it as
given below:
tempArrays.sortByProperties("weight","height");

I am not able to get the code to work. I keep getting an "undefined is
null or not an object" error. Also, I need for the code to work in IE.
We don't support netscape. Kindly help me.

Thanks once again.

Lasse Reichstein Nielsen <lr*@hotpop.com> wrote in message news:<ad**********@hotpop.com>...
ei************@hotmail.com (Seeker) writes:
I used the following function to sort.

function sortBy(prop,arr) {
sortProp=prop;
arr=arr.sort(sortFunc);
}


You use a global variable to keep the property to sort by. Instead you
could use a local sortFunc function that can see the "prop" variable
directly. That is safer in a multithreaded environment.
This however allows me to sort only by one criteria. I need to sort by
weight first and then by height for those items that have the same
weight. Any help in the form of algorithms or code snippets will be
highly appreciated.


If you want to be generic:

Array.prototype.sortByProperties = function sortByProperties() {
var propArr = arguments; // not sure IE 4 understands. NS4 does.
this.sort(function(a,b) {
for(var i=0;i<propArr.length;i++) {
var ap = a[propArr[i]], bp = b[propArr[i]];
if (ap < bp) {return -1;}
if (ap > bp) {return 1;}
}
return 0;
})
return this;
};

You can then use it to sort by the properties you want:

arr.sortByProperties("weight","height");

or even

arr.sortByProperties("weight","height","code");

/L

Jul 20 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Pratt | last post by:
I have been using the following for sorting a list of dictionaries. This works but only provides sorting on a single key. I am wanting to extend this with a better comparison expression so that it...
7
by: hank | last post by:
Hi All In the Circular Logging when the Primary Log file fill up, the database manager will creat a secondary log files for the transaction; when this transaction finished, the secondary log...
3
by: Dale Lundgren | last post by:
I have a c# class library that launches a Win Form in a secondary thread. From the Form (now running in the secondary thread) I need to be able to start a method that is defined in the class and...
6
by: Michelle Konzack | last post by:
Hello all, I have accidently :-) found 'initlocation' and now I like to know, how many secondary database i can create. I like to do that, because I have a Virtual Webserver and for each...
4
by: Zorpiedoman | last post by:
In a multi-monitor environment, a form I am creating at run time keeps placing itself on the primary screen, even if the main form of the program making the call is on the secondary screen. How...
5
by: (PeteCresswell) | last post by:
User's screen has a "Sort By" option group on it with radio buttons for things like "Deal Name", "Collateral Manager", "Underwriter", Closing Date", "Holding Size", and so-on and so-forth. But...
0
by: veg_all | last post by:
I think I figured out how to use array_multisort to sort by a primary index. But how can i make it sort by a secondary index?
2
by: Ultrak The DBA | last post by:
We have logarchmeth1 set. Also, we have a monitoring script that checks the gap between any log files in the active log directory. We have just recently activated logarchmeth1 and what we have...
14
by: deppeler | last post by:
my flat field DB fields are: I have them sorted by field (category=TeachersActivity or ChildrensBooks) so they are grouped together as Children's Books then Teacher Activities open(BASE,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.