?
Solved

sort($array) problem

Posted on 2004-11-22
4
Medium Priority
?
201 Views
Last Modified: 2010-05-18
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
Comment
Question by:Vampireofdarkness
4 Comments
 
LVL 3

Expert Comment

by:y2kwacko
ID: 12651501
$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

by:gruntar
ID: 12652189
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

by:
bljak earned 400 total points
ID: 12652205
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

by:Vampireofdarkness
ID: 12654260
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question