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