How to make auto index sort files with numbers like 1-1000

Started on the old forum in August 2014 · view the original in the archive · old links redirect here

5 replies · 260 views · started 12 years ago · 1 min read

4 participants
monsp3 Developer · 57 posts #1

Hi,

Its been a while since I posted and visited this site, a lot have changed. Anyhow I have encountered a problem and I was like searching the web but can't find the right words to search.

I have encountered a problem in auto index scripts when sorting A-Z and if the files have numbers like example Naruto episode 1 - 1000. I kinda sorts like FF: Naruto episode 1 Naruto episode 10 up to 20 Naruto Episode 2 Naruto episode 21-30 Naruto 3

Something like that, is there a way to make it sort in right order? I dunno if I am making sense. T_T

arnage Lead · 811 posts #2

Hi.

Just rename Naruto episode 1 to Naruto episode 01, 2 to 02 and so on... 🙂

👍 1
Share
<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
something else Fellow · 2,296 posts #3

Here is a sort function i created for you 😛

<?
$vids = array('N 1','N 10','N 11','N 12','N 13','N 14','N 15','N 16','N 17','N 18','N 19','N 2','N 20','N 21','N 22','N 23','N 24','N 25','N 26','N 27','N 28','N 29','N 3','N 30','N 31', 'N 4', 'N 5', 'N 6', 'N 7', 'N 8', 'N 9');

function sortedBySE($a, $b)
{
    $a = explode(' ', $a);
    $b = explode(' ', $b);
    $c = (count($a) > count($b)) ? count($a) : count($b);
    foreach(range(0, $c) as $i)
    {
        if($a[$i] < $b[$i])
        {
            return -1;
        }
        else if($a[$i] > $b[$i])
        {
            return 1;
        }
    }
    return -1;
}

usort($vids, "sortedBySE");
print_r($vids);
?>

It may need slight work to make it work better for you - but at least you have something to work with 🙂

👍 1
Share
morse Senior · 364 posts #4

I am unable to understand the function that you have provided ... You populate an array $vids but then in the function you carry out check match and sort on $a , $b .. also when you implement usort function is it not supposed to be

[COLOR=#000000][COLOR=#0000BB]usort[/COLOR][COLOR=#007700]([/COLOR][COLOR=#0000BB]$vids[/COLOR][COLOR=#007700], [/COLOR][COLOR=#DD0000]"sortedBySE()"[/COLOR][COLOR=#007700]);[/COLOR][/COLOR]

This function provided by you is confusing!

tinyurl.com/earnbymobile Easy earning for Indians

Alternative mobile advertising network .. Minimum 100 USD pay / NET15 pay cycle, Good Brand, Best targeting for Android goo.gl/6vub3

something else Fellow · 2,296 posts #5

Nope it is meant to be like i have wrote it 😊

Its because no variables can be included inside that function (due to usort passing 2 of the array items at a time to the user created function (a,b) and comparing the 2 and then called as many times as it is needed in an algorithm until the whole array is sorted.

If you do need to pass extra variables to the user function then rather than use global variables you can use use

and yes confusing >.< and i am probably not making it easier to understand with this post lol

something else Fellow · 2,296 posts #6

something else wrote: Here is a sort function i created for you 😛

<?
$vids = array('N 1','N 10','N 11','N 12','N 13','N 14','N 15','N 16','N 17','N 18','N 19','N 2','N 20','N 21','N 22','N 23','N 24','N 25','N 26','N 27','N 28','N 29','N 3','N 30','N 31', 'N 4', 'N 5', 'N 6', 'N 7', 'N 8', 'N 9');

function sortedBySE($a, $b)
{
$a = explode(' ', $a);
$b = explode(' ', $b);
$c = (count($a) > count($b)) ? count($a) : count($b);
foreach(range(0, $c) as $i)
{
if($a[$i] < $b[$i])
{
return -1;
}
else if($a[$i] > $b[$i])
{
return 1;
}
}
return -1;
}

usort($vids, "sortedBySE");
print_r($vids);
?>

It may need slight work to make it work better for you - but at least you have something to work with 🙂

Im looking at this trying to work out why i put range( ) in this >.<

It could also do with some alphabetical sorting also

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close