avinashse
avinashse

Reputation: 1460

Sorting in Structure on the basis of its first vector element

I have an structure consisting of two vector arrays.

struct hotel {
  vector<int> start_time[1000],end_time[1000];
};

I have to sort the structure on the basis o start_time in such a way that end_time. For E.g.,

start_time[0] has 4 elements:
  start_time[0] = 12 10 8 9
  end_time[0]   = 100 20 30 50

start_time[1] has 5 elements:
  start_time[1] = 100 23 50 10 32
  end_time[1]   =  40 20 10 15 34

so output will be:

start_time[0] = 8 9 10 12
end_time[0]   = 30 50 20 100

start_time[1] = 10 23 32 50 100
end_time[1]   = 15 20 34 10 40

Please guide me in this regard.

Thank You

I found 1 more thing, if instead of declaring vector arrarys I use this:

struct hotel {
    vector<int> start_time,end_time;
}h[1000];

will also server my purpose but now I have h[0] instead of start_time[0] and end_time[0]. but having the same problem how to sort h[i].start_time but not h[i].end_time. I am trying to think like tony's solution, using of pair. thank you for the replies.

Upvotes: 0

Views: 122

Answers (2)

Tony Delroy
Tony Delroy

Reputation: 106086

#include <algorithm>

// create a container storing associated pairs of start and end times...
std::vector<std::pair<int,int>> times;

for (int v = 0; v < 1000; ++v) // vector to be ordered on this iteration...
{
    assert(my_hotel.start_time[v].size() == my_hotel.end_time[v].size());

    // populate times...
    for (int i = 0; i < my_hotel.start_time[v].size(); ++i)
        times.push_back(std::make_pair(my_hotel.start_time[v][i], my_hotel.end_time[v][i]));

    // sort it...
    std::sort(times.begin(), times.end());

    // copy sorted data back into hotel structure...
    for (int i = 0; i < times.size(); ++i)
    {
        my_hotel.start_time[v][i] = times[i].first;
        my_hotel.end_time[v][i] = times[i].second;
    }

    times.clear();
}

The above could be done more declaratively with e.g. std::copy and lambdas, but I don't personally see much value in doing so.

Upvotes: 1

Kamran Khan
Kamran Khan

Reputation: 1366

Here is the code.

#include <iostream>
#include <vector>

using namespace std;

int main(int argc, const char * argv[])
{
    const int vectorSize = 2;
    vector<int> start_time[vectorSize];
    vector<int> end_time[vectorSize];

    //element at index 0
    start_time[0] = {12, 10, 8, 9};
    end_time[0]   = {100, 20, 30, 50};

    //element at index 1
    start_time[1] = {100, 23, 50, 10, 32};
    end_time[1]   =  {40, 20, 10, 15, 34};

    //Here is what you need
    //Make sure that both start_time and end_time have same size, which will be in this case
    for(int i = 0; i < vectorSize; i++) //This will work on start_time, end_time indexes
    {
        //This will sort each vectore - I am using bubble sort method
        for(int v = 0; v < start_time[i].size(); v++)
        {
            for(int k = 0; k < start_time[i].size() - 1; k++)
            {
                if(start_time[i][k] > start_time[i][k + 1])
                {
                    int temp = start_time[i][k];
                    start_time[i][k] = start_time[i][k + 1];
                    start_time[i][k + 1] = temp;

                    int temp2 = end_time[i][k];
                    end_time[i][k] = end_time[i][k + 1];
                    end_time[i][k + 1] = temp2;
                }
            }
        }
    }

    for(int i = 0; i < vectorSize; i++) 
    {
        cout<<"start_time["<<i<<"]: ";
        for(int k = 0; k < start_time[i].size(); k++)
        {
            cout<<start_time[i][k]<<" ";
        }

        cout<<endl;

        cout<<"end_time["<<i<<"]: ";
        for(int k = 0; k < end_time[i].size(); k++)
        {
            cout<<end_time[i][k]<<" ";
        }

        cout<<endl;

    }



    return 0;
}

Upvotes: 0

Related Questions