how to order an array by a particular rule or a function?

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

    how to order an array by a particular rule or a function?

    I'd like to roder an array:
    $array[id][name]
    $array[id][number]
    $array[id][value]

    I'd like to order $array by name or by numbre of by a
    function($array[id1],$array[id2]) that return 1 or -1 if id1 is less tha
    id2 or viceversa...
    How can I do that?
    How can I order in a complex case like this?
    Thank you in advance,
    Andrea.


  • Richard Cornford

    #2
    Re: how to order an array by a particular rule or a function?

    _andrea.l wrote:[color=blue]
    > I'd like to roder an array:
    > $array[id][name]
    > $array[id][number]
    > $array[id][value][/color]

    Is this an array? How is it created, and its values assigned?
    [color=blue]
    > I'd like to order $array by name or by numbre of by a
    > function($array[id1],$array[id2]) that return 1 or -1
    > if id1 is less tha id2 or viceversa...[/color]

    What are id1 and id2, and in what sense do they have magnitudes?
    [color=blue]
    > How can I do that?[/color]

    You have not provided sufficient information to have that question
    answered.
    [color=blue]
    > How can I order in a complex case like this?[/color]
    <snip>

    Nothing presented so far suggest a complex case here.

    Richard.


    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: how to order an array by a particular rule or a function?

      "_andrea.l" <andrea.lorizAN TISPAM@libero.i t> writes:
      [color=blue]
      > I'd like to roder an array:
      > $array[id][name]
      > $array[id][number]
      > $array[id][value]
      >
      > I'd like to order $array by name or by numbre of by a
      > function($array[id1],$array[id2]) that return 1 or -1 if id1 is less tha
      > id2 or viceversa...[/color]

      I assume "$array" is an array, and "id" refers to number indices.

      Then you can sort using the "sort" method of arrays, which takes an
      optional comparison function:
      ---
      function cmpName(v1,v2) {
      if (v1.name < v2.name) { return -1; }
      else if (v1.name > v2.name) { return 1; }
      else {return 0;}
      }

      $array.sort(cmp Name); // now sorted by name
      ---

      More generally, you can use these functions:
      ---
      /* Compares anything that is recognized by "<" */
      function cmp(v1,v2){
      return (v2<v1)-(v1<v2); // tricky code, but it works
      }

      /* creates a function that compares objects by ther "propName" property */
      function cmpProperty(pro pName) {
      return function(v1,v2) {
      return cmp(v1[propName],v2[propName]);
      }
      }

      $array.sort(cmp Property("name" )); // sorted by name property
      $array.sort(cmp Property("numbe r")); // sorted by number property
      ---

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      Working...