Hi ~

Before posting my code, can someone tell me if it is possible to do a dual sort on an array of chars and an array of int, double or float? Would you have to use two separate sorts? Really confused here.

I am trying to sort an array of 12 doubles (user input) so that the console output sorts it in descending order. The corresponding months should be printed as well.

Thanks for any help! Super lost on this one.
:?:

ps - Have a separate function for both arrays. The functions are called in main.

Recommended Answers

All 5 Replies

If the format of one array determines the output of the other it might just be better to have a 'mapping' from one to the other. For instance, to map ints to strings you might do

std::map< int, std::string > months;
months[1] = "January";
months[2] = "February";
// ... and so on

That way, you can just sort your value array and use it to display the mapping. For instance

std::list< int > int_list;
// ... get input
// ... sort list
std::list< int >::iterator lit = int_list.begin ();
for (; lit != int_list.end (); ++lit) 
   std::cout << "Value: " << *lit << " -> Month: " << months[*lit] << std::endl;

Of course, you will need to verify user input and handle other such details but that gives you the main idea.

Yes, it's completely possible. Set up an index array idx . Then month[idx[2]] and dblval[idx[2]] will be a matched pair.

During the sort, check arrayEntry[idx[i]] with arrayEntry[idx[i+1]] and if they need to be switched, switch only idx[i] and idx[i+1] . The actual arrays stay in their original order, only the index array gets sorted.

If the format of one array determines the output of the other it might just be better to have a 'mapping' from one to the other. For instance, to map ints to strings you might do

std::map< int, std::string > months;
months[1] = "January";
months[2] = "February";
// ... and so on

That way, you can just sort your value array and use it to display the mapping. For instance

std::list< int > int_list;
// ... get input
// ... sort list
std::list< int >::iterator lit = int_list.begin ();
for (; lit != int_list.end (); ++lit) 
   std::cout << "Value: " << *lit << " -> Month: " << months[*lit] << std::endl;

Of course, you will need to verify user input and handle other such details but that gives you the main idea.

Your idea of mapping looks great however I have not gone that far in my studies. I am still a 'super novice'. My code looks nothing like std::'anything' yet. Hopefully by the end of the semester yes? Anyway, thanks for your patience and advice. I still am not able to associate the string array with the numbered months array however I have the program working perfectly despite that flaw.

Yes, it's completely possible. Set up an index array idx . Then month[idx[2]] and dblval[idx[2]] will be a matched pair.

During the sort, check arrayEntry[idx[i]] with arrayEntry[idx[i+1]] and if they need to be switched, switch only idx[i] and idx[i+1] . The actual arrays stay in their original order, only the index array gets sorted.

Hmmmm...still lost darn it. I did get the dualSort function working properly and it sorts both arrays though both are integer arrays and not string arrays. I would really love to know how to associate the two or change the int months[] to a string array that could be compared. Is there a conversion method using static_cast or something?

I see the thread is solved-and just I want to know the same thing...could you maybe post the conclusion?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.