GetDotted Domains

Viewing Thread:
"PHP Sorting a array"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Sat 06/07/02 at 16:34
Regular
Posts: 787
Hi i've been playing around with all the different sort array functions but cant get it to work the way i want.

This is how inside my array looks:

blah[0] = 60#cough
blah[1] = 43#blah
blah[2] = 100#woof
blah[3] = 3#moo

..etc

How could i get it to sort the by the numbers before the #?
Sat 06/07/02 at 16:34
Posts: 0
Hi i've been playing around with all the different sort array functions but cant get it to work the way i want.

This is how inside my array looks:

blah[0] = 60#cough
blah[1] = 43#blah
blah[2] = 100#woof
blah[3] = 3#moo

..etc

How could i get it to sort the by the numbers before the #?
Sat 06/07/02 at 17:38
Posts: 0
T0MMY wrote:
> Hi i've been playing around with all the different sort array
> functions but cant get it to work the way i want.
>
> This is how inside my array looks:
>
> blah[0] = 60#cough
> blah[1] = 43#blah
> blah[2] = 100#woof
> blah[3] = 3#moo
>
> ..etc
>
> How could i get it to sort the by the numbers before the #?

not very easily, coz it would think that blah[0] should come after blah[3] coz it would see that 6 > 1.

how about using a different array structure?

try this instead:

blah[60] = cough
blah[43] = blah
blah[100] = woof
blah[3] = moo

PHP will simply fill in the missing $blah entries as NULL
Sat 06/07/02 at 21:07
Posts: 0
Ahhh didnt think of that cheers.
Sun 07/07/02 at 05:15
Posts: 0
ah that wont work i forgot that some values may be the same i just used them random numbers as an example it could be..

blah[0] = 3#cough
blah[1] = 43#blah
blah[2] = 3#woof
blah[3] = 3#moo

...or anything really there any other ways around this?
Sun 07/07/02 at 15:33
Posts: 0
There's a way in Perl of creating a function to do the 'test' part of the sorting, so within that function you can do anything you like (as you normally do). The function gets passed a and b (the values the sort routine is currently checking), and you return 1 for a>b, 0 for a=b, or -1 for a
The languages are reasonably similar, so it's possible that PHP has a similar function. Do a google search for 'custom sort function in PHP' or some similar search.

HTH,
Rikki
Sun 07/07/02 at 23:37
Posts: 0
rik wrote:
> in Perl you use = for equals in number comparisons

nearly, you use == for numerical comparison. = is used to set a variable or array etc...

as for custom sort operations, i think you might be correct. the function you want is: usort()

ive never used it before, but here is the example from the PHP manual:

function cmp ($a, $b) {
if ($a == $b) return 0;
return ($a > $b) ? -1 : 1;
}

$a = array (3, 2, 5, 6, 1);

usort ($a, "cmp");

while (list ($key, $value) = each ($a)) {
echo "$key: $value\n";
}

-----------------

ur definitely correct about splitting it around the #. try this:

for ($i=0; $i<=count($blah); $i++)
{ $blah2[$i] = explode("#", $blah[$i]); }

// you can then call the number using $blah[$i][0] i believe
Mon 08/07/02 at 00:13
Posts: 0
Nimco wrote:
> rik wrote:
> in Perl you use = for equals in number comparisons
>
> nearly, you use == for numerical comparison. = is used to set a
> variable or array etc...

Yeh, mistype :-) You can check those minor details as you code ;-)

> as for custom sort operations, i think you might be correct. the
> function you want is: usort()
>
> ive never used it before, but here is the example from the PHP
> manual:
>
> function cmp ($a, $b) {
> if ($a == $b) return 0;
> return ($a $b) ? -1 : 1;
> }
>
> $a = array (3, 2, 5, 6, 1);
>
> usort ($a, "cmp");
>
> while (list ($key, $value) = each ($a)) {
> echo "$key: $value\n";
> }
>
> -----------------
>
> ur definitely correct about splitting it around the #. try this:
>
> for ($i=0; $i<=count($blah); $i++)
> { $blah2[$i] = explode("#", $blah[$i]); }
>
> // you can then call the number using $blah[$i][0] i believe

Yup, the OP just needs to combine those two things to create his custom function, which explodes his array value, then compares the numbers and returns appropriately.

Rikki
Mon 08/07/02 at 07:53
Posts: 0
Rikki wrote:
> Yup, the OP just needs to combine those two things to create his
> custom function, which explodes his array value, then compares the
> numbers and returns appropriately.
>
> Rikki

I think the problem he is gonna have is that when you split a string, it returns strings. this means that it will not sort them numerically, but literally, thus it would think for example: 3 > 100 since 3 > 1.

to avoid this, use settype(), e.g. settype($blah[$i], "int");

:)
Mon 08/07/02 at 14:14
Regular
"Devil in disguise"
Posts: 3,151
Seems to be a much simpler solution to this.

sort($blah,SORT_NUMERIC);

Just tried it on the test data given and it does produce

blah[0]=3#moo
blah[1]=43#blah
blah[2]=60#cough
blah[3]=100#woof

It works because of how PHPs (and PERLs for that matter) string->number conversion behaves. Typically, it looks at the string and just keeps reading numbers until it encounters the end or a non numeric character hence "3#moo" -> 3 and so on..

A more general point, don't be so caught up in trying to solve things with php functions. They are there for convenience but they aren't the beginning and the end of the language by a long way. You all seem to have expended alot of effort trying to use builtin functions when you could of written the code yourselves in 6 lines (a simple sort routine is nothing fancy, 2 nested for loops and a comparison in the middle).

-G
Tue 09/07/02 at 00:28
Posts: 0
Aren't the built-in sort routines faster and more optimized than one you'd write yourself. To start with, you'd have to pick the best sort for your own function (bubble, quick, whatever else you can think of), and try and make it as quick as possible, where as the built-in sort is all done for you. Agreed there's some things easier to do yourself than search the tons of built-in functions for what you want, but surely sort is one you should leave to the built-ins...?

Rikki

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Unrivalled services
Freeola has to be one of, if not the best, ISP around as the services they offer seem unrivalled.
Many thanks!
You were 100% right - great support!

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.