While jpadie's suggestion is the best alternative, you could also change the for loop to foreach, concatenate the sn and givenname fields and use that as the key, then just do an asort of the array when you're done. You can loop through the sorted array and output the data afterward.
If your...