Sorting Problem HELp

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 2
Reputation: zsady is an unknown quantity at this point 
Solved Threads: 0
zsady zsady is offline Offline
Newbie Poster

Sorting Problem HELp

 
0
  #1
Aug 4th, 2009
Hi I have a vector which contains a number of objects. Inside these objects are 2 attributes. the X and the Y coordinates. How Can I sort them from top to bottom 1st then left to right?


e.g

0,0
1,0
0,5,
3,1
2,5

the output would be
0,0
0,5
1,0
2,5
3,1

since this needs to check not 1 but 2 attributes, im a bit stumped. Any help would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 677
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 101
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: Sorting Problem HELp

 
0
  #2
Aug 4th, 2009
I guess it is just a question of grouping.

The Basic Approach would be like this:

First you will need to start with sorting the "X" coordinates. After that. Group all the Objects having the same "X" coordinate. After that. In that particular group ... Sort the "Y" coordinates.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Sorting Problem HELp

 
1
  #3
Aug 4th, 2009
Sorting concept is simple: from the least to the greatest or from the greatest to the least. To do so, you need to do comparison. So, the magic trick is comparison-rule (which is greater and which is lesser). For example: we got 5 points (consisting of x-axis and y-axis).

  1. X, Y
  2. Point-A (5, 10)
  3. Point-B (4, 6)
  4. Point-C (6, 7)
  5. Point-D (5, 9)
  6. Point-E (4, 9)

And then I create my comparison-rule:
If  Point1.X is greater than Point2.X then  Point1.X is greater than Point2
If  Point1.X is equal to Point2.X then 
   | If Point1.Y is greater than Point2.Y  then Point1 is great than Point2
   | If Point1.Y is lesser than Point2.Y  then Point1 is lesser than Point2
If  Point1.X is lesser than Point2.X then  Point1.X is lesser than Point2

Using this comparison with some comparison-process. I have finally sorted my points.
Result after sort
           X, Y
Point-C   (6, 7)
Point-A   (5, 10)
Point-D   (5, 9)
Point-E   (4, 9)
Point-B   (4, 6)

Now, time to change my comparison-rule so that I would get difference result:

New comparison-rule:
If Point1.X + Point1.Y > Point2.X + Point2.Y Then  Point1 is greater than Point2
Else  Point1 is lesser than Point2.

After do comparison-process with this new rule, I got difference result:
Result after sort
           X, Y
Point-A   (5, 10)  // 5 + 10 = 15
Point-D   (5, 9)   // 5 + 9 = 14
Point-C   (6, 7)    // 6 + 7 = 13
Point-E   (4, 9)   // 4 + 9 = 13
Point-B   (4, 6)   // 4 + 6 = 10
Yesterday is a history, tomorrow is a mystery, today is a gift.
Behind every smile is a tear.
Visal .In
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,408
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 181
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso

Re: Sorting Problem HELp

 
0
  #4
Aug 4th, 2009
use the std::sort algo. you will need to give it your compare function
which should compare the x first then the y.

example :
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4.  
  5. using std::vector;
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9.  
  10.  
  11. int main()
  12. {
  13.  
  14. vector< vector<float> > vec2d(2,vector<float>(2,0));
  15.  
  16. for(int i =0; i < vec2d.size(); i++)
  17. {
  18. cout<<"Enter "<<vec2d[i].size()<<" numbers for col "<<i<<"\n";
  19.  
  20. for(int j = 0; j < vec2d[i].size(); j++)
  21. {
  22.  
  23. cin >> vec2d[i][j];
  24. }
  25.  
  26. }
  27.  
  28. std::sort(vec2d.begin(),vec2d.end());
  29.  
  30. for(int i =0; i < vec2d.size(); i++)
  31. {
  32.  
  33. for(int j = 0; j < vec2d[i].size(); j++)
  34. {
  35.  
  36. cout<<vec2d[i][j]<<" ";
  37. }
  38.  
  39. cout<<"\n";
  40. }
  41. }

You will have to adjust it though.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC