开发者

Sorting an array according to other array

开发者 https://www.devze.com 2023-02-25 09:13 出处:网络
I have an array with arrays of two elements. Now I would like to sort all values ending at zero (e.g. arr[3][0], arr[1][0]) to be sorted from low to high.

I have an array with arrays of two elements. Now I would like to sort all values ending at zero (e.g. arr[3][0], arr[1][0]) to be sorted from low to high.

Then I would like to have the values ending at 1 (e.g. arr[2][1], arr[1][1]) to also be sorted, but not on its own order, but in the same order as the first array.

Here is what I tried:

int compareInts(const void* a, const void* b)
{
    return ( *(int*) a[0] - *(int*) b[0] );
}

int a开发者_C百科rr[4][2];

arr[0][0] = 50;
arr[0][1] = 0;

arr[1][0] = 40;
arr[1][1] = 1;

arr[2][0] = 50;
arr[2][1] = 2;

arr[3][0] = 85;
arr[3][1] = 3;

qsort( arr, 4, sizeof(int), compareInts );

I would like to have the following result:

arr[0][0] = 40;
arr[0][1] = 1;

arr[1][0] = 50;
arr[1][1] = 0;

arr[2][0] = 50;
arr[2][1] = 2;

arr[3][0] = 85;
arr[3][1] = 3;


Just implement your own search algorithm (use bubble sort or whatever you think might be most efficient) and do the comparison/swapping similar to the following pseudo code:

if(a[i][0] > a[j][0])
{
    t[0] = a[i][0];
    t[1] = a[i][1];
    a[i][0] = a[j][0];
    a[i][1] = a[j][1];
    a[j][0] = t[0];
    a[j][1] = t[1];
}

If you'd like to sort based on multiple collumns you'd just have to repeat this comparing other sub array elements and sorting the least significant column first.

Edit: I thik this should also be possible using qsort(). You just have to set the element size accordingly (should be 2 * sizeof(int) in your example). Leave the rest of your code unchanged (although I'm not sure about this and can't test run it right now).


One way to do this:

using namespace std;
struct Compare
{
    bool operator()(const pair<int,int>& p1,
                    const pair<int,int>& p2)
    {
        return p1.first < p2.first;
    }
};

int main()
{
    int arr[4][2];

    arr[0][0] = 50;
    arr[0][1] = 0;

    arr[1][0] = 40;
    arr[1][1] = 1;

    arr[2][0] = 50;
    arr[2][1] = 2;

    arr[3][0] = 85;
    arr[3][1] = 3;

    //Create a vector of pairs
    vector<pair<int,int> > pairs;
    for(int  i = 0; i < 4; ++i)
    {
        pairs.push_back(make_pair(arr[i][0], arr[i][1]));
    }

    //Sort the vector on the first element using the functor
    stable_sort(pairs.begin(), pairs.end(), Compare());

    //Copy the result back
    for(size_t idx = 0; idx < pairs.size(); ++idx)
    {
        arr[idx][0] = pairs[idx].first;
        arr[idx][1] = pairs[idx].second;
    }
    return 0;
}


Do you have to have an array of type int[][]? If not, you could do something like the following (more or less like Asha answered, I was already typing it up when his answer appeared).

#include <algorithm>
#include <utility>

struct cmpTwoIntsPair
{
  bool operator() (pair<int, int> const & lhs, pair<int, int> const & rhs)
  {
    return lhs.first < rhs.first;
  }
}

typedef std::pair<int, int> twoInts;

unsigned int arrSize = 5;
twoInts arr[arrSize];

... Insert values here ...

std::sort(arr, arr + arrSize, cmpTwoIntsPair);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号