Sorting structures

Hi, I'm a beginner with C++ and am trying to make a programme that will sort a list of names and mobile numbers, depending on which is chosen to be sorted. I'm going to try and use the standard sort() function in the algorithm header file, but I'm not sure how to go about sorting by name or surname or mobile number.
My struct is

1
2
3
4
5
6
struct data{
	char first_name[max];
	char surname[max];
	int phone_number[number_length];

};


I've also declared my array of structures with

 
	struct data *d = new data[N]


I've tried looking around online and I can't seem to get my head around the explanations given. Do I need to write a predicate (?) function to compare different elements in my array, if so in what sense are they compared? I thought the sort function would just compare and sort them all itself?

Any help would be greatly appreciated
You need to tell std::sort how to compare items.
The comparison function has to have the meaning of 'less than'.
If your struct has the < operator overloaded, std::sort will use that as comparison, otherwise you have to pass a specific function or class
Since you want to choose whether to sort by name or by number you should write the two comparison functions accordingly.
Topic archived. No new replies allowed.