As I can see, you use two separate arrays here. I think, you should use one array, which element is structure and sort it.