0

I am trying to sort the elements in 2d array like this:

338 640 723.771 62.1603

364 804 882.56 65.642

199 664 693.179 73.3166

I need to sort them regards to 3rd and 4th columns.

For instance for the 3rd column:

199 664 693.179 73.3166

338 640 723.771 62.1603

364 804 882.56 65.642

For the 4th column:

338 640 723.771 62.1603

364 804 882.56 65.642

199 664 693.179 73.3166

I hope I could explain what I want to do. Thanks for help already..


Answer:

I've found what need it. I am putting the codes here maybe it could be helpful for someone else.

this is compare function for columns:

bool Compare(vector<double> A, vector<double> B) {
    return (A[2] < B[2]); // 2 means which column that you want to compare

}

and this is the sorting code:

std::sort(dots.begin(), dots.end(), &Compare); // "dots" needs to be a vector. in this case its a 2d double vector

the source is : https://stackoverflow.com/a/37516971/5331586

2
  • related/dupe: stackoverflow.com/questions/20304553/… Commented Apr 21, 2017 at 16:02
  • It seems a multivariable objective function. How do you handle the following case: r1 and r2 are the two row of a matrix; r1[2] < r2[2] but r2[3] < r2[3]? (Indices starts from 0) Commented Apr 21, 2017 at 16:16

1 Answer 1

-1

Use std::sort with your own comparator. I would solve it like this:

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

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

class Comparate2DArrayByColumn {
public:
    Comparate2DArrayByColumn(int column)
        : column(column)
    {
    }
    template <typename T>
    bool operator()(const std::vector<T>& array1, const std::vector<T>& array2)
    {
        // do not use [] here, it will be UB
        return array1.at(column) > array2.at(column);
    }

private:
    int column;
};

void printArray(const std::vector<std::vector<double> >& array)
{
    for (auto& line : array) {
        for (auto val : line) {
            cout << val << " ";
        }
        cout << endl;
    }
}

int main()
{
    std::vector<std::vector<double> > array = {
        { 33, 640, 723.771, 62.1603 },
        { 364, 804, 882.56, 65.642 },
        { 199, 664, 693.179, 73.3166 },
    };

    printArray(array);
    cout << endl
         << endl;

    std::sort(array.begin(), array.end(), Comparate2DArrayByColumn(2));

    printArray(array);

    return 0;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Well, it wasn't really useful and should have been a comment until you added an example.
Also Comparate2DArrayByCollumn should not be templated and instead operator() should be templated. That way you don't need to do Comparate2DArrayByCollumn<double>(2) but can instead use Comparate2DArrayByCollumn(2)
That takes into account just the third column, what about the fourth?
@biagio-festa use std::sort(array.begin(), array.end(), Comparate2DArrayByCollumn<double>(3);
@devalone then the sorting respect to (2) will be lost :D
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.