Jump to content


Photo

Sorting values in an array


  • Please log in to reply
4 replies to this topic

#1 Harfish

Harfish
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 26 May 2006 - 03:53 AM

I'm not even sure if this is possible but here goes.

I have a website that uses PHP to produce a list of names from a MySQL database. However, a basic design decision I made a long time ago seems to have some back to haunt me. The names in the database are in the format 'J Bloggs' and when these are sorted into alphabetical order they are sorted by first initial. Is there a simple way to sort these names by last name? I've tried a couple of things, but neither of those worked quite right. I've tried splitting the names into an associative array, eg J => Bloggs, but this didn't work because some of the last names are the same.

Am I going to have to redesign my database? Or is it possible to sort and array by the third character of each value?

#2 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 26 May 2006 - 05:23 AM

Hmmm... If you can split them into array, then you can use one of the array sorting function. I am a bit lazy to find what you need, but one of them should work:

uksort(), usort(), uasort(), sort(), asort(), arsort(), ksort(), natsort(), and rsort().

EDIT:

In your case, asort() should do it:
[a href=\"http://www.php.net/asort\" target=\"_blank\"]http://www.php.net/asort[/a]
~ D Kuang

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 May 2006 - 09:43 AM

As long as they all have that same format of a single initial and lastname

SELECT SUBSTRING(name, 3) as lastname, 
            SUBSTRING(name,1,1) as initial  
FROM mytablename
ORDER BY surname, initial

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 Harfish

Harfish
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 31 May 2006 - 02:05 AM

Wow Barand! Thanks, that worked beautifully. But now I've discovered I've got another problem. I have another SQL query on that page that selects 2 names and generates a list. Up until a few weeks ago this list was horribly out of order because when I wrote the php initially I had neither the skill nor the time to fix that. Now I do and have the list sorted by first initial.

$today = date("Y-m-d");
$result = mysql_query("SELECT DISTINCT name1,name2 FROM names WHERE date>='$today' AND tjs='Y'",$db);

$tjs =  array();
while ($myrow = mysql_fetch_array($result)) {
$tj1 = $myrow['name1'];
$tj2 = $myrow['name2'];
array_push($tjs,$name1);
array_push($tjs,$name2);
}
sort($tjs);
foreach ($tjs as $value){
printf("<option value=\"$value\">$value</option>\n");
}

But I can't for the life of my figure out how to sort this list by surname, which is the 3rd character of the name. Any suggestions?

#5 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 31 May 2006 - 06:24 AM

Use a custom sort function with usort().
function namesort($a, $b) {
     return strcmp(substr($a,2), substr($b,2));
}

usort ($tjs, 'namesort');

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users