Solved

sort(\$array) problem

Posted on 2004-11-22
198 Views
I have an array that is filled with 58 character numbers

0000000000000000000000000000000000000000000000000000000000

When I attempt to sort() using info from php.net the order is random. Not any numerical order at all.

I have tried

==============================

sort(\$arr)

==============================

sort(\$arr, SORT_NUMERICAL)

==============================

sort(\$arr) reset(\$arr)

==============================

sort(\$arr, SORT_NUMERICAL) reset(\$arr)

==============================

usort(\$arr)

==============================

function cmp(\$a, \$b) {
if(\$a==\$b) { return 0; }
return (\$a<\$b)?-1:1;
}
usort(\$arr, "cmp");

==============================

but neither work.

Thanks
0
Question by:Vampireofdarkness

LVL 3

Expert Comment

\$arr = array("1","9","7","3","8","6","8","4","2","1","5","9","6","2","5","8","3","2","6","8","9","2","3","4");
sort(\$arr);
print_r(\$arr);

worked for me so I am not sure where are differences are I would probably say use the cmp function method and force the type to be an int. The method below also worked fine for me.

\$arr = array("1a","9a","7a","3a","8a","6a","8a","4a","2a","1a","5a","9a","6a","2a","5a","8a","3a","2a","6a","8a","9a","2a","3a","4a");
sort(\$arr);

function cmp(\$a, \$b) {
if((int)\$a==(int)\$b) { return 0; }
return ((int)\$a<(int)\$b)?-1:1;
}
usort(\$arr, "cmp");

print_r(\$arr);
0

LVL 9

Expert Comment

Try this, each time you reload page you get values mixed

<?php

srand((float) microtime() * 10000000);
\$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
// count array length
\$x = count(\$input);
//make rand keys;
\$rand_keys = array_rand(\$input, \$x);

foreach (\$rand_keys AS \$k => \$v){
echo '<br>' . \$input[\$v];
}
?>

Cheers
0

LVL 4

Accepted Solution

hmm yes
lets see
SORT_NUMERICAL is not working, strange ..
try using flags SORT_STRING or SORT_REGULAR which both worked for me

\$arr[0] = "0000000000000000000000000000000000000000000000000000000010";
\$arr[1] = "0000000000000000000000000000000000000000000000000000000000";
\$arr[2] = "0000000000000000000000000000000000000000000000000000000001";
sort(\$arr, SORT_STRING);
var_dump(\$arr);

array(3) {
[0]=>
string(58) "0000000000000000000000000000000000000000000000000000000000"
[1]=>
string(58) "0000000000000000000000000000000000000000000000000000000001"
[2]=>
string(58) "0000000000000000000000000000000000000000000000000000000010"
}

//bljak
0

LVL 9

Author Comment

The SORT_NUMERICAL wasn't working. But the SORT_STRING does. I didn't think to try it because I didn't want to assume my numbers weren't numerical XD

Thus, bljak gave me the solution and will receive points -- I never did get what gruntar was proving?

Thanks for the help.
0

Featured Post

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…