Solved

# order a list numerically

Posted on 2013-06-13
204 Views
0
Question by:rgb192
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 31

Expert Comment

ID: 39244267
You can try this:
``````<?php
error_reporting('E_ALL');

\$arr = array(
"http://website.com/folder/page.php?u=12853",
"http://website.com/folder/page.php?u=21684",
"http://website.com/folder/page.php?u=1550",
"http://website.com/folder/page.php?u=14754",
"http://website.com/folder/page.php?u=9912",
"http://website.com/folder/page.php?u=14577",
"http://website.com/folder/page.php?u=21252",
"http://website.com/folder/page.php?u=15503",
"http://website.com/folder/page.php?u=10548",
"http://website.com/folder/page.php?u=15190",
);
echo "<pre>";
var_dump(\$arr);
echo "</pre>";
foreach(\$arr as \$a){
\$data= explode("=", \$a);
\$numbers[] = \$data[1];
}
sort(\$numbers);
foreach(\$numbers as \$n){
\$sortedArray[] = "http://website.com/folder/page.php?u=" . \$n;
}

echo "<pre>";
var_dump(\$sortedArray);
echo "</pre>";
``````

Cheers
0

LVL 110

Accepted Solution

Ray Paseur earned 500 total points
ID: 39244290
PHP has a built-in function for this.
http://php.net/manual/en/function.natsort.php

And if you're looking for alphabetic sorting, this one is often the best answer.
http://php.net/manual/en/function.natcasesort.php

``````<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo "<pre>";

\$arr = array
( "http://website.com/folder/page.php?u=12853"
, "http://website.com/folder/page.php?u=21684"
, "http://website.com/folder/page.php?u=1550"
, "http://website.com/folder/page.php?u=14754"
, "http://website.com/folder/page.php?u=9912"
, "http://website.com/folder/page.php?u=14577"
, "http://website.com/folder/page.php?u=21252"
, "http://website.com/folder/page.php?u=15503"
, "http://website.com/folder/page.php?u=10548"
, "http://website.com/folder/page.php?u=15190"
)
;
var_dump(\$arr);

natsort(\$arr);
print_r(\$arr);
``````
Best regards, ~Ray
0

LVL 15

Expert Comment

ID: 39257366
echo '<pre>';

\$u  = array("12853","21684","1550","14754","9912","14577","21252","15503","10548","15190");

sort(\$u);

foreach(\$u as \$key=>\$val)
{
\$arr[] = "http://website.com/folder/page.php?u=".\$val;
}

print_r(\$arr);
0

Author Closing Comment

ID: 39261188
This code sorts the urls with easy function

first code sorts parsed numbers

last code sorts only numbers and requires copy paste
0

## Featured Post

Question has a verified solution.

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

Build an array called \$myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
###### Suggested Courses
Course of the Month5 days, 3 hours left to enroll