I think the easiest way it to not assicate them by names but rather id's,
do a join and then sort by name
if you are unfamiliar with JOINS, they are definately worth a read.
For example. lets say you take your same friend schemes but normalize it
table users
ID | Username
"1" | "tommy
"2" | "jerry"
"3" | "linda"
table friends
ID | Users_ID | Friends_ID
1 | 1 | 2
2 | 1 | 3
3 | 2 | 3
SELECT `c`.* FROM `users` AS `a`
LEFT JOIN `friends` AS `b` ON `a`.`id` IN (`b`.`user_id`, `b`.`friend_id`)
LEFT JOIN `users` AS `c` ON `c`.`id` IN (`b`.`user_id`, `b`.`friend_id`)
WHERE `a`.`username` = "$username"
right now it will return you you $username as well, but you can tweak that until it's okay