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.

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.

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).

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

And then I create my comparison-rule:

[B]If [/B] Point1.X is greater than Point2.X [B]then[/B]  Point1.X is greater than Point2
[B]If [/B] Point1.X is equal to Point2.X [B]then[/B] 
   | [B]If[/B] Point1.Y is greater than Point2.Y  [B]then[/B] Point1 is great than Point2
   | [B]If[/B] Point1.Y is lesser than Point2.Y  [B]then[/B] Point1 is lesser than Point2
[B]If [/B] Point1.X is lesser than Point2.X [B]then[/B]  Point1.X is lesser than Point2

Using this comparison with some comparison-process. I have finally sorted my points.

[B]Result after sort[/B]
           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:

[B]If[/B] Point1.X + Point1.Y > Point2.X + Point2.Y [B]Then[/B]  Point1 is greater than Point2
[B]Else[/B]  Point1 is lesser than Point2.

After do comparison-process with this new rule, I got difference result:

[B]Result after sort[/B]
           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
Comments
Good explanations.

use the std::sort algo. you will need to give it your compare function
which should compare the x first then the y.

example :

#include<iostream>
#include<vector>
#include<algorithm>

using std::vector;
using std::cout;
using std::cin;
using std::endl;


int main()
{  

	vector< vector<float> > vec2d(2,vector<float>(2,0));

	for(int i =0; i < vec2d.size(); i++)
	{
		cout<<"Enter "<<vec2d[i].size()<<" numbers for col "<<i<<"\n";

		for(int j = 0; j < vec2d[i].size(); j++)
		{

			cin >> vec2d[i][j];			
		}

	}

	std::sort(vec2d.begin(),vec2d.end());
	
	for(int i =0; i < vec2d.size(); i++)
	{
 
		for(int j = 0; j < vec2d[i].size(); j++)
		{

			cout<<vec2d[i][j]<<" ";
		}
		
		cout<<"\n";
	}		
}

You will have to adjust it though.

This article has been dead for over six months. Start a new discussion instead.