By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,946 Members | 741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,946 IT Pros & Developers. It's quick & easy.

sortign string - why this difference?

P: n/a
Hi!

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

for($i=0;$i<(count($item1)-1);$i++)
{
$k = $i;
for($j=$i;$j<(count($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

Jun 15 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Sonnich wrote:
Hi!

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

for($i=0;$i<(count($item1)-1);$i++)
{
$k = $i;
for($j=$i;$j<(count($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


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

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

Sonnich

Jun 15 '06 #2

P: n/a
Sonnich,

Most likelly your an error here:
for($j=$i;$j<(count($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/

Jun 15 '06 #3

P: n/a

AlexVN wrote:
Sonnich,

Most likelly your an error here:
for($j=$i;$j<(count($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.


stupid me.... thanks

Sonnich

Jun 16 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.