If you are going to create a function template to sort an array, I would recommend that you follow the same signature as the std::sort function. The problem with your function template is that it is not generic enough, it only applies to a C-style array of types which can be compare with > (greater-than) operator. If you look at std::sort, it doesn't suffer any of these problems, it applies to any container or array type and it allows a special comparison function to be provided.
template <typename BidirIterator, typename CompareFunction>
void insSort(BidirIterator first, BidirIterator last, CompareFunction compare)
{
BidirIterator i = first; ++i;
for(; i != last ; ++i)
{
typename std::iterator_traits<BidirIterator>::value_type key = *i; // in C++11: auto key = std::move(*i);
BidirIterator j = i;
BidirIterator j_prior = j; --j_prior;
while((j != first) && (compare(key, *j_prior)))
{
*j = *j_prior; // in C++11: *j = std::move(*j_prior);
--j; --j_prior;
}
*j = key; // in C++11: *j = std::move(key);
}
};
template <typename BidirIterator>
void insSort(BidirIterator first, BidirIterator last) {
insSort(first,last,std::less< typename std::iterator_type<BidirIterator>::value_type >());
};
So, for your problem, you should either create a < less-than operator overload for your Name class, or provide another comparison function to the insSort call.
As for calling insSort with an array, like you have, you just do:
insSort( &nameArray[0], &nameArray[0] + sizeof(nameArray) / sizeof(nameArray[0]) );
mike_2000_17
21st Century Viking
3,135 posts since Jul 2010
Reputation Points: 2,050
Solved Threads: 625
Skill Endorsements: 41