Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4586584
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:39:34+00:00 2026-05-21T21:39:34+00:00

I have a set of points (x,y,z). I want to sort these data using

  • 0

I have a set of points (x,y,z). I want to sort these data using first column and 2nd and 3rd columns should be rearranged according to the sorting 1st column. is it possible to do this in c++, if so could you pls help me.

Herewith I am attaching codes of my implementation but I got a error message “invalid conversion from ‘const’ vod*’ to ‘const int [*][3]’ “ in line 31 and 32. I tried this using several methods but my effort was not success yet. I used here ‘qsort’ for this, are there any other methods or can I use ‘sort’ in to do this. Since I have a very big data set I wish to use a fast method.
So what I need at the end the data set which is sorted using only 1st column like following example:
before sort

34  12  12
12  34 15
24  20  34
13  11  10
40  23  32

after sort

12  34 15
13  11  10
24  20  34
34  12  12
40  23  32

if any good methods help me to write codes …thanks

#include <iostream>
#include <cstdlib>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class Point
{
   private:          
    double x;
    double y;
    double z;

   public:
     Point(){};
     ~Point(){};

   Point(double X, double Y, double Z){
     x=X;y=Y;z=Z; }

   double X(){return x;}
   double Y(){return y;}
   double Z(){return z;}
};

int cmp ( const void *pa, const void *pb ) {
const int (*a)[3] = pa;
const int (*b)[3] = pb;
if ( (*a)[1] < (*b)[1] ) return -1;
if ( (*a)[1] > (*b)[1] ) return +1;
return 0;
}

int main ( ) {
vector<Point> points;
int input_x,input_y,input_z;       
   int i=0;
   while(i<6){//data set,it is a example, actual data come from a file 

         cout<<"x: ";cin>>input_x;
         cout<<"y: ";cin>>input_y;
         cout<<"z: ";cin>>input_z;
         Point point(input_x,input_y,input_z);                        
         points.push_back(point);
        i++;                         
         }        
for (int i=0;i<points.size();i++){//before sort
cout<<points[i].X()<<" "<<points[i].Y()<<" "<<points[i].Z()<<endl;
}

qsort( points, 6, sizeof points[0], cmp );

for (int i=0;i<points.size();i++){//after sort
cout<<points[i].X()<<" "<<points[i].Y()<<" "<<points[i].Z()<<endl;
}            
system("PAUSE");
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T21:39:35+00:00Added an answer on May 21, 2026 at 9:39 pm

    It’s almost certainly easiest to use std::sort instead of qsort:

    class Point { 
        int x, y, z;
    public:
        Point(int x, int y, int z) : x(x), y(y), z(z) {}
    
        bool operator<(Point const &other) { 
            return x < other.x;
        }
        // skipping the reading and other stuff for now...
    };
    
    int main() { 
        std::vector<Point> points;
        // add some Points to `points` here.
    
        // sort using order defined in Point::operator<:
        std::sort(points.begin(), points.end());
        return 0;
    }
    

    Edit: to keep the comparison separate from the items being compared, you use a separate function or functor to do the comparison, and pass that to std::sort. There are a few things about your class that you really want to change in any case though — at the very least, since your Point::X(), Point::Y() and Point::Z() don’t modify the Point object, you want to make them const member functions. Once you’ve done that, the sorting is fairly trivial:

    #include <vector>
    #include <algorithm>
    #include <iterator>
    #include <iostream>
    
    class Point { 
        double x, y, z;
    public:
        double X() const { return x; }
        double Y() const { return y; }
        double Z() const { return z; }
    
        Point(double x=0.0, double y=0.0, double z=0.0) : x(x), y(y), z(z) {}
    };
    
    namespace std { 
    ostream &operator<<(ostream &os, Point const &p) { 
        return os << "(" << p.X() << ", " << p.Y() << ", " << p.Z() << ")";
    }
    }
    struct byX { 
        bool operator()(Point const &a, Point const &b) { 
            return a.X() < b.X();
        }
    };
    
    int main(){ 
        std::vector<Point> points;
    
        for (int i=0; i<10; i++)
            points.push_back(Point(rand(), i, i));
    
        std::cout << "Unsorted:\n";
        std::copy(points.begin(), points.end(), 
            std::ostream_iterator<Point>(std::cout, "\n"));
    
        std::sort(points.begin(), points.end(), byX());
    
        std::cout << "\nSorted:\n";
        std::copy(points.begin(), points.end(), 
            std::ostream_iterator<Point>(std::cout, "\n"));
        return 0;
    }
    

    Technically, I suppose I should add one more minor detail: if the x value in any of your points is a NaN, this won’t work correctly. A NaN isn’t equal to anything (not even itself) which violates the strict weak ordering required for std::sort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of points that i want to turn into a closed
I have set of points which lies on the image. These set of points
I have a set of points represented as a 2 row by n column
I have a set of data points in 3D space which apparently all fall
I have a set of X,Y data points (about 10k) that are easy to
So, i have a set of points (x,y), and i want to be able
I have a random set of points and want to create a smooth svg
Say I have a set of data points that are represented as an array
I have set break points on my attached properties SetXXX and GetXXX static methods.
I have a set S of points (2D : defined by x and y)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.