Issue with vector<vector<int>> sorting

Hi everyone,

I have a vector which contains vectors containing 7 integers each. I'd like to sort these vectors based on the value of the first integer (int IOT), in ascending order. I know this type of question can be found everywhere, but I'm lost as to why this doesn't compile. Any help is greatly appreciated.

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <windows.h>



using namespace std;

class orders {
public:
    int IOT; // Incoming Order Time
    int ORD; // ORDer
    int NOB; // Number Of Bikes
    int STA; // STAtion
    int TWO; // Time Window Open
    int TWC; // Time Window Close
    int SVT; // SerVice Time

    bool operator < (const orders& o){
    	return IOT < o.IOT;
    	}
};



int main(){

vector<orders> all_orders;

// Order vectors chronologically before further processing
sort (all_orders.begin(), all_orders.end());

return 0;}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <algorithm>
#include <vector>

class orders 
{
public:
    int IOT; // Incoming Order Time
    int ORD; // ORDer
    int NOB; // Number Of Bikes
    int STA; // STAtion
    int TWO; // Time Window Open
    int TWC; // Time Window Close
    int SVT; // SerVice Time

    bool operator < (const orders& o){
        return IOT < o.IOT;
    }
};

int main()
{

    std::vector<orders> all_orders;

    // Order vectors chronologically before further processing
    std::sort (all_orders.begin(), all_orders.end());

    return 0;
}


Compiles fine for me.

However, it is not what is described in the subject of the thread or in:
I have a vector which contains vectors containing 7 integers each. I'd like to sort these vectors based on the value of the first integer (int IOT), in ascending order.
Hi, thanks for the reply. Hmm maybe there's something wrong with my compiler.

But for you the code sorts for example:

((1,2,3,4,5,6,7),(0,1,2,3,4,5,6),(4,5,6,7,8,9,10),(2,3,4,5,6,7,8))

Like ((0,1,2,3,4,5,6),(1,2,3,4,5,6,7),(2,3,4,5,6,7,8)(4,5,6,7,8,9,10)) based on the first integer in each vector?

I'm new to programming, but I have to pick this up for a project I'm working on. So maybe the subject of the thread and my terminology may be wrong.
Last edited on
You mention vector<vector<int>> in your post but I don't see it in your code.

So basically, you'd like to "sort" the data members inside the orders class? Or what?
Yes, sorry for the wrong title. You are right, I'd like to sort it
But for you the code sorts for example:

((1,2,3,4,5,6,7),(0,1,2,3,4,5,6),(4,5,6,7,8,9,10),(2,3,4,5,6,7,8))

Like ((0,1,2,3,4,5,6),(1,2,3,4,5,6,7),(2,3,4,5,6,7,8)(4,5,6,7,8,9,10)) based on the first integer in each vector?


Except for the part about being based on the first integer in each vector (as there is only one vector,) yes.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <algorithm>
#include <vector>

class orders 
{
public:
    int IOT; // Incoming Order Time
    int ORD; // ORDer
    int NOB; // Number Of Bikes
    int STA; // STAtion
    int TWO; // Time Window Open
    int TWC; // Time Window Close
    int SVT; // SerVice Time

    bool operator < (const orders& o){
        return IOT < o.IOT;
    }
};

const unsigned nOrders = 4 ;
orders ordersInfo[nOrders] =
{
    {1, 2, 3, 4, 5, 6, 7},
    {0, 1, 2, 3, 4, 5, 6},
    {4, 5, 6, 7, 8, 9, 10},
    {2, 3, 4, 5, 6, 7, 8}
};

std::ostream & operator<<( std::ostream& os, const orders& order )
{
    os << "{ " << order.IOT << ' ' << order.ORD << ' ' << order.NOB
        << ' ' << order.STA << ' ' << order.TWO << ' ' << order.TWC
        << ' ' << order.SVT << " }" ;
    return os ;
}

std::ostream& operator<<(std::ostream& os, const std::vector<orders> & o)
{
    for ( auto it = o.begin(); it != o.end(); ++it )
        os << *it << '\n' ;
    return os ;
}

int main()
{

    std::vector<orders> all_orders(ordersInfo, ordersInfo+nOrders);

    std::cout << "Before sort: \n" << all_orders << '\n';

    std::sort (all_orders.begin(), all_orders.end());

    std::cout << "After sort: \n" << all_orders << '\n' ;
}
Before sort:
{ 1 2 3 4 5 6 7 }
{ 0 1 2 3 4 5 6 }
{ 4 5 6 7 8 9 10 }
{ 2 3 4 5 6 7 8 }

After sort:
{ 0 1 2 3 4 5 6 }
{ 1 2 3 4 5 6 7 }
{ 2 3 4 5 6 7 8 }
{ 4 5 6 7 8 9 10 }
Thank you very much :)
closed account (D4S8vCM9)
For me it seems to work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <algorithm>
#include <vector>
#include <iostream>

class orders 
{
public:
    int IOT; // Incoming Order Time
    int ORD; // ORDer
    int NOB; // Number Of Bikes
    int STA; // STAtion
    int TWO; // Time Window Open
    int TWC; // Time Window Close
    int SVT; // SerVice Time

    bool operator < (const orders& o) const {
        return IOT < o.IOT;
    }
};

int main()
{

    std::vector<orders> all_orders;

    orders a, b, c;
    a.IOT = 3; a.ORD = 1;
    b.IOT = 9; b.ORD = 2;
    c.IOT = 1; c.ORD = 3;
    
    all_orders.push_back(a);
    all_orders.push_back(b);
    all_orders.push_back(c);
    
    for(std::vector<orders>::iterator i = all_orders.begin(); i != all_orders.end(); ++i) {
      std::cout << "IOT: " << (*i).IOT << " ORD: " << (*i).ORD << std::endl;
    }
    
    // Order vectors chronologically before further processing
    std::sort (all_orders.begin(), all_orders.end());
    
    for(std::vector<orders>::iterator i = all_orders.begin(); i != all_orders.end(); ++i) {
      std::cout << "IOT: " << (*i).IOT << " ORD: " << (*i).ORD << std::endl;
    }

    return 0;
}

Output:
IOT: 3 ORD: 1
IOT: 9 ORD: 2
IOT: 1 ORD: 3
IOT: 1 ORD: 3
IOT: 3 ORD: 1
IOT: 9 ORD: 2
Topic archived. No new replies allowed.