building and sorting an array