You're are thinking the wrong way round, starting with the idea that you need an array. You have not said what language you are programming in so I will use C as it's my first language (and English is my second :D).
You need to start with what you want to store for each person (name and age).
Additionally you are going to have move than 1 and you need to be able to store these in a list and this list needs to be sorted into a specific order.
To hold the data you could us a structure like this
-
struct person {
-
char surname[20];
-
unsigned int age;
-
};
-
Now you have said that you need an array however I do not think that is the best solution. Consider the size of the structure, 24 bytes in this case. Assume that in order to keep the ordering of the list you need to insert a person into the 4th position.
Now if your array currently contains 10 entries then you will need to move 7 entries up, at 24 bytes each that is 168 bytes of data to move. Suppose your list contains 400 entries, you will need to copy 397 entries or 9528 bytes, also suppose that rather than 24 bytes the structure is 150 bytes, then you will be copying 59550 bytes.
As you can see this solution is not scaleable, that is as the structure size and array size gets bigger more and more work is required to insert (or delete) items. AT some point the data will get too large to make this a sensible solution.
A better solutition is a linked list. In a linked list the data structure contains a pointer to the next structure in the list. What this means is that inserting (or deleting) an item from the list is much easier. Taking the example above to insert a new person into the list all I have to do is alter the pointer in the 3rd item to point to the new item and the pointer in the new item to point to the item that the 3rd item was pointing to. This is 2 pointer assignments or 8 bytes copied and it will be the same no matter how big the structure or how long the list. This solution is scalable.
The structure would be altered to
-
struct person {
-
struct person *pNext;
-
char surname[20];
-
unsigned int age;
-
};
-
Another advantage is that if you have more than 1 order that you want the list stored in than you can just add another pointer to the structure and link the new pointers in a different order.
In this sort of solution it is normal for the top level data to just be a pointer to the structure being stored and to use malloc to create additional structures as require (this also gets round the problem of having to create an array large enough for the data). When freeing the structures remember that you can use data once you have freed it, normally this means reading the next pointer into a temporary variable then freeing the structure.