Secondary sort

Giganews Newsgroups
Subject: Secondary sort
Posted by:  eightbits1by…@hotmail.com (Seeker)
Date: 2 Sep 2003

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.

Replies