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.
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 #?
> 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
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
> 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
> 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
> 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");
:)
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
Rikki