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 #?
Mon 15/07/02 at 13:19
Regular
"whoneedsatagline?"
Posts: 194
In one of your previous posts, you said that you could have the same numbers on more than one array entry ...

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

So, what does the numeric sort do with the three array entries starting with '3'? I'd guess that it would just ignore the words. Maybe you don't care about having the words alpha sorted within the numbers, though.
Sun 14/07/02 at 06:27
Posts: 0
Garin wrote:
> 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

Ah cheers it works. Im sure i tried that but didnt work i think its because i done:

$blah = sort($blah,SORT_NUMERIC);

:\ thanks again
Fri 12/07/02 at 13:01
Regular
"whoneedsatagline?"
Posts: 194
What I'd do is to make sure that the numeric bit before the # is always a fixed number of digits, using sprintf() or str_pad() for example. So it would look like this, for example

102#stan
003#pete
002#bert
016#fred

Then the array will sort just fine, and you can still treat the first n characters as a number when you want to.
Tue 09/07/02 at 01:13
Regular
"Devil in disguise"
Posts: 3,151
The built in functions will usually be faster of course if only because the code for them is precompiled. But speed really isn't much of an issue on operations like this unless you're sorting 1000s.

The point I was trying to make was, solutions don't begin and end with the built in functions. People have seemingly expended effort trying to get the sorts functions to do as they wanted when they could of just as easily written the code themselves at a performance loss they probably wouldn't even notice.

-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
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
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 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
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
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

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.
The coolest ISP ever!
In my opinion, the ISP is the best I have ever used. They guarantee 'first time connection - everytime', which they have never let me down on.

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.