sortign string - why this difference?

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

    #1

    sortign string - why this difference?

    Hi!

    I have 2 arrays which I sort ny the first one.

    for($i=0;$i<(co unt($item1)-1);$i++)
    {
    $k = $i;
    for($j=$i;$j<(c ount($item1)-1);$j++);
    {
    if (strcmp($item1[$k],$item1[$j])<0)
    $k = $j;
    }
    if($k<>$i)
    {
    $temp=$item1[$i]; $item1[$i]=$item1[$k];
    $item1[$k]=$temp;
    $temp=$item2[$i]; $item2[$i]=$item2[$k];
    $item2[$k]=$temp;
    }
    }

    Pretty simple - it works, I get the highest item first - I'd like it
    the other way.

    But
    if (strcmp($item1[$j],$item1[$k])<0) // k an j switched
    does not work, though
    if (strcmp($item1[$j],$item1[$k])>0) // < replaced by >
    does work, but it is descending

    why this?

    I can read my array backwards, it is ok, but why this????

    BR
    Sonnich

  • Sonnich

    #2
    Re: sortign string - why this difference?


    Sonnich wrote:[color=blue]
    > Hi!
    >
    > I have 2 arrays which I sort ny the first one.
    >
    > for($i=0;$i<(co unt($item1)-1);$i++)
    > {
    > $k = $i;
    > for($j=$i;$j<(c ount($item1)-1);$j++);
    > {
    > if (strcmp($item1[$k],$item1[$j])<0)
    > $k = $j;
    > }
    > if($k<>$i)
    > {
    > $temp=$item1[$i]; $item1[$i]=$item1[$k];
    > $item1[$k]=$temp;
    > $temp=$item2[$i]; $item2[$i]=$item2[$k];
    > $item2[$k]=$temp;
    > }
    > }
    >
    > Pretty simple - it works, I get the highest item first - I'd like it
    > the other way.
    >
    > But
    > if (strcmp($item1[$j],$item1[$k])<0) // k an j switched
    > does not work, though
    > if (strcmp($item1[$j],$item1[$k])>0) // < replaced by >
    > does work, but it is descending
    >
    > why this?
    >
    > I can read my array backwards, it is ok, but why this????
    >
    > BR
    > Sonnich[/color]

    I should add the the strings I have are something like:

    AB123456
    BB345324
    BB235342
    XX3634543
    T44444 <- this is rare, but might happen
    TR000001

    Sonnich

    Comment

    • AlexVN

      #3
      Re: sortign string - why this difference?

      Sonnich,

      Most likelly your an error here:
      for($j=$i;$j<(c ount($item1)-1);$j++);
      This "for" is an empty "for" that just does nothing, because its body
      is empty--it is closed by the the last ";" in the line.

      --
      Alexander,
      http://www.alexatnet.com/

      Comment

      • Sonnich

        #4
        Re: sortign string - why this difference?


        AlexVN wrote:[color=blue]
        > Sonnich,
        >
        > Most likelly your an error here:
        > for($j=$i;$j<(c ount($item1)-1);$j++);
        > This "for" is an empty "for" that just does nothing, because its body
        > is empty--it is closed by the the last ";" in the line.[/color]

        stupid me.... thanks

        Sonnich

        Comment

        Working...