[PHP] Sort by string length...

2004-12-21 Thread Russell P Jones
Any idea how to sort an array by string length?

Russ Jones

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] Sort by string length...

2004-12-21 Thread Michael Sims
Russell P Jones wrote:
 Any idea how to sort an array by string length?

Use usort() in conjunction with a user defined function that compares the 
length of
both strings using strlen().  If brevity at the (possible) expense of clarity is
your thing, you can even use create_function() as your callback argument to 
usort()
to make this a one-liner.  Of course if that were your sort of thing you'd 
probably
be using perl instead of PHP. :)

HTH

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sort by string length...

2004-12-21 Thread Tom Rogers
Hi,

Wednesday, December 22, 2004, 7:18:52 AM, you wrote:
RPJ Any idea how to sort an array by string length?

RPJ Russ Jones


With a user defined sorting function something like this

?php
function cmp($a, $b){
  $x = strlen($a);
  $y = strlen($b);
  if ($x == $y) {
return 0;
  }
  return ($x  $y) ? -1 : 1;
}
$array = array('Longest string','short','longer');
usort($array,'cmp');
?

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sort by string length...

2004-12-21 Thread Jason Wong
On Wednesday 22 December 2004 05:18, Russell P Jones wrote:
 Any idea how to sort an array by string length?

usort(), strlen()

-- 
Jason Wong - Gremlins Associates - www.gremlins.biz
Open Source Software Systems Integrators
* Web Design  Hosting * Internet  Intranet Applications Development *
--
Search the list archives before you post
http://marc.theaimsgroup.com/?l=php-general
--
/*
panic(do_trap: can't hit this);
linux-2.6.6/arch/i386/mm/extable.c
*/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sort by string length...

2004-12-21 Thread Chris
One of the Array sort functions?
http://www.php.net/usort
Russell P Jones wrote:
Any idea how to sort an array by string length?
Russ Jones
 

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] Sort by string length...

2004-12-21 Thread ApexEleven
How about iterating though the strings and putting string values and
lengths in an array:

[begin untested code]
$strings = array('i','am','who');
$string_count = array();
foreach ($strings as $string) {
 $string_count['value'][] = $string;
 $string_count['length'][] = strlen($string);
}
array_multisort($string_count['length'],SORT_ASC,SORT_NUMERIC,$string_count['value']);

[/end untested code]

This'll take an array of strings and sort them from shortest to longest.

There may be some other function already made, this was just off the
top of my head.


On Tue, 21 Dec 2004 16:18:52 -0500 (EST), Russell P Jones
[EMAIL PROTECTED] wrote:
 Any idea how to sort an array by string length?
 
 Russ Jones
 
 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php
 
 


-- 

Jasper Howard - Database Administration
ApexEleven.com
530 559 0107
---

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sort by string length...

2004-12-21 Thread tg-php
Is it an associative array?  That is, are you assigning values like this:

Method 1:
$arr[] = some text string;
or
$arr = array(some text string,some text string too);

or like this...

Method 2:
$arr[value1] = some text string;
or
$arr1 = array(value1=some text string,value2=some text string too);


If you do it the first way, Method 1 (non-associative) then you could do this:

?php
$arr1 = array(some text string,some text string too);

foreach ($arr1 as $value) {
  $length = strlen($value);
  $tmparr[$length][] = $value;
}

sort($tmparr);
reset($tmparr);

foreach ($tmparr as $lenarr) {
  foreach ($lenarr as $value) {
$arr2[] = $value;
  }
}

?

Now $arr2 has a string length sorted list of your values.. or something close 
to it.  I may have a little syntax off a bit, but you get the idea.

I don't know of any way to actually just sort by string length, you'll have 
your make your own function like the one above.   Just remember that if you use 
$tmparr[$length]  that you may have multiple strings of the same length which 
will overwrite each other if you don't do another layer like I did (with 
$tmparr[$length][] = $value);

Just gotta get a little clever with it.

Good luck!

-TG


= = = Original message = = =

Any idea how to sort an array by string length?

Russ Jones

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


___
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Sort by string length...

2004-12-21 Thread David Otton
On Tue, 21 Dec 2004 16:18:52 -0500 (EST), you wrote:

Any idea how to sort an array by string length?

You write a function that compares two strings (A and B), and returns
0 if len(A) == len(B), -1 if len(A)  len(B) or +1 if len(A)  len(B).

function compare_by_length ($a, $b)
{
$la = strlen ($a);
$lb = strlen ($b);
if ($la == $lb) return (0);
return ($a  $b) ? -1 : 1;
}

Then, you pass the array to be sorted and the comparison function to
one of the  u*sort() functions:

usort ($array, 'compare_by_length');

(Caution: untested code. Also, consider what happens when you pass a
jagged array to usort()).

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php