Connecting Tech Pros Worldwide Forums | Help | Site Map

multi sorting in c++

Newbie
 
Join Date: Oct 2009
Posts: 3
#1: Oct 20 '09
I'm trying to sort array like in this code
Expand|Select|Wrap|Line Numbers
  1. int vysledok[4];
  2. string vysledok1[4];
  3. vysledok[0] = 12;
  4. vysledok1[0] = "patrick";
  5. vysledok[0] = 18;
  6. vysledok1[0] = "bear";
  7. vysledok[0] = 82;
  8. vysledok1[0] = "rabit";
  9. vysledok[0] = 12;
  10. vysledok1[0] = "pet";
  11.  
and i need have this result
Expand|Select|Wrap|Line Numbers
  1. 82: rabit
  2. 18: bear
  3. 12: patrick
  4. 12: pet
so i need sort array vysledok(DESC) and next i have to have sorted(array vysledok1 > ASC) text like at line 3,4(in result) it can't be!!!
Expand|Select|Wrap|Line Numbers
  1. 12: pet
  2. 12: patrick

Could someone help me?




Sorry for my bad ENGLISH

myusernotyours's Avatar
Familiar Sight
 
Join Date: Nov 2007
Posts: 168
#2: Oct 20 '09

re: multi sorting in c++


All that code you put there is just some declarations which is not even correct. You can't have two variable with the same names like that. We need to see what you have done so far and you should explain your problem clearly. That way you will get help.

Regards,

Alex.
Newbie
 
Join Date: Oct 2009
Posts: 3
#3: Oct 20 '09

re: multi sorting in c++


i need sort array vysledok (DESC) (so i need to get 82,16,12,12)
and when program found same numbers, its possible to sort words(ASC)

AND to code i can't send it here i can send it only as Instant messeage
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,171
#4: Oct 21 '09

re: multi sorting in c++


Look closely myusernotyours there is a '1' difference in the variable names.

However patqo you have not got your assignments right, they are all to the first element of the arrays, however I think I probably understand what you mean.

The sorting in your first post is not alphabetical, you have rabit (rabbit) before bear.

You are making a relatively common mistake, there is relationship between the members of vysledok and the members of vysledok1, a 1 to 1 correlation but you have not expressed that in your code you are using 2 completely unrelated arrays.

Typically the way we express that relationship is to put the data in a class (or structure) and have an array of classes. Of course this is C++ so we prefer not to use arrays but rather have a vector of our classes.

Finally you want to sort the vector into order. So sorting vectors is not very efficient so at this point you have to ask if you could use a list rather than a vector. However a list doesn't support random access (via at() or operator[]() member functions) so if you need random access you stick with the vector.

Finally you need to sort the vector (or list) so you implement a comparison operator, operator<(...) which allows either the sort algorithm for vectors or the sort member function for lists to be used. In the operator< you define precisely how to tell if one class instance is less than or greater than another based on the value of its member attributes.


And finally if you instant message your code somewhere you can post it here since both just involve a copy/paste out of the source file.
Newbie
 
Join Date: Oct 2009
Posts: 3
#5: Oct 21 '09

re: multi sorting in c++


...
sorry i have an error in input
Expand|Select|Wrap|Line Numbers
  1. int vysledok[4];
  2. string vysledok1[4];
  3. vysledok[0] = 12;
  4. vysledok1[0] = "patrick";
  5. vysledok[1] = 18;
  6.  vysledok1[1] = "bear";
  7. vysledok[2] = 82;
  8.  vysledok1[2] = "rabit";
  9.  vysledok[3] = 12;
  10.  vysledok1[3] = "pet";
Familiar Sight
 
Join Date: Jan 2007
Posts: 191
#6: Oct 22 '09

re: multi sorting in c++


Assuming both arrays are initialized with the strings and ints you have shown...use say the bubble sort or sort or sort_heap algorithm on both arrays to sort them. I believe you need to include the algorithm header. Once each array is sorted use a for loop to print out the corresponding elements of each array.
Expand|Select|Wrap|Line Numbers
  1. for(int i=0;i<4;i++)
  2. cout<<array1[i]<<array2[i]<<endl;
Reply

Tags
c++, multisort, sort