Secondary sort

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Seeker

    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(so rtFunc);
    }


    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.
  • Lasse Reichstein Nielsen

    #2
    Re: Secondary sort

    eightbits1byte@ hotmail.com (Seeker) writes:
    [color=blue]
    > I used the following function to sort.
    >
    > function sortBy(prop,arr ) {
    > sortProp=prop;
    > arr=arr.sort(so rtFunc);
    > }[/color]

    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.
    [color=blue]
    > 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.[/color]

    If you want to be generic:

    Array.prototype .sortByProperti es = function sortByPropertie s() {
    var propArr = arguments; // not sure IE 4 understands. NS4 does.
    this.sort(funct ion(a,b) {
    for(var i=0;i<propArr.l ength;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.sortByPrope rties("weight", "height");

    or even

    arr.sortByPrope rties("weight", "height","code" );

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Seeker

      #3
      Re: Secondary sort

      Hi,
      Thank you for the prompt response. I pasted the code and called it as
      given below:
      tempArrays.sort ByProperties("w eight","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 <lrn@hotpop.com > wrote in message news:<ad9mitzm. fsf@hotpop.com> ...[color=blue]
      > eightbits1byte@ hotmail.com (Seeker) writes:
      >[color=green]
      > > I used the following function to sort.
      > >
      > > function sortBy(prop,arr ) {
      > > sortProp=prop;
      > > arr=arr.sort(so rtFunc);
      > > }[/color]
      >
      > 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.
      >[color=green]
      > > 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.[/color]
      >
      > If you want to be generic:
      >
      > Array.prototype .sortByProperti es = function sortByPropertie s() {
      > var propArr = arguments; // not sure IE 4 understands. NS4 does.
      > this.sort(funct ion(a,b) {
      > for(var i=0;i<propArr.l ength;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.sortByPrope rties("weight", "height");
      >
      > or even
      >
      > arr.sortByPrope rties("weight", "height","code" );
      >
      > /L[/color]

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Secondary sort

        eightbits1byte@ hotmail.com (Seeker) writes:
        [color=blue]
        > Thank you for the prompt response. I pasted the code and called it as
        > given below:
        > tempArrays.sort ByProperties("w eight","height" );[/color]

        I tried it in IE6 with just:

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

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

        /L
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        • Seeker

          #5
          Re: Secondary sort

          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.


          eightbits1byte@ hotmail.com (Seeker) wrote in message news:<fb57159b. 0309030654.1462 c586@posting.go ogle.com>...[color=blue]
          > Hi,
          > Thank you for the prompt response. I pasted the code and called it as
          > given below:
          > tempArrays.sort ByProperties("w eight","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 <lrn@hotpop.com > wrote in message news:<ad9mitzm. fsf@hotpop.com> ...[color=green]
          > > eightbits1byte@ hotmail.com (Seeker) writes:
          > >[color=darkred]
          > > > I used the following function to sort.
          > > >
          > > > function sortBy(prop,arr ) {
          > > > sortProp=prop;
          > > > arr=arr.sort(so rtFunc);
          > > > }[/color]
          > >
          > > 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.
          > >[color=darkred]
          > > > 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.[/color]
          > >
          > > If you want to be generic:
          > >
          > > Array.prototype .sortByProperti es = function sortByPropertie s() {
          > > var propArr = arguments; // not sure IE 4 understands. NS4 does.
          > > this.sort(funct ion(a,b) {
          > > for(var i=0;i<propArr.l ength;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.sortByPrope rties("weight", "height");
          > >
          > > or even
          > >
          > > arr.sortByPrope rties("weight", "height","code" );
          > >
          > > /L[/color][/color]

          Comment

          Working...