| | |
Sorting Problem HELp
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2009
Posts: 2
Reputation:
Solved Threads: 0
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.
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.
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).
And then I create my comparison-rule:
Using this comparison with some comparison-process. I have finally sorted my points.
Now, time to change my comparison-rule so that I would get difference result:
New comparison-rule:
After do comparison-process with this new rule, I got difference result:
C++ Syntax (Toggle Plain Text)
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:
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
Behind every smile is a tear.
Visal .In
use the std::sort algo. you will need to give it your compare function
which should compare the x first then the y.
example :
You will have to adjust it though.
which should compare the x first then the y.
example :
C++ Syntax (Toggle Plain Text)
#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.
![]() |
Similar Threads
- Sorting problem once again.... (Java)
- Sorting problem (MS SQL)
- java sorting problem (Java)
- sorting problem (C++)
- Sorting problem ? (ASP.NET)
- Question: Linear Time Sorting Problem (Computer Science)
- Collapsible table sorting problem (JavaScript / DHTML / AJAX)
- Link List Sorting Problem (C++)
Other Threads in the C++ Forum
- Previous Thread: how do I connect to SQL with c++
- Next Thread: Send data to child thread (for safe termination)
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






