-3

I have three String arrays

A: [-22, -3, 2.2, a]
B: [-22, -3, 2.2, b]
C: [-22, 0, 2.2]

After sorting I want to get this sequence

C: [-22, 0, 2.2]
A: [-22, -3, 2.2, a]
B: [-22, -3, 2.2, b]

The comparison between a pair of arrays is done on the first element in the arrays. If the element is identical, the second element, and so on.

What comparator must be used?

RealSkeptic
  • 33,993
  • 7
  • 53
  • 79
dmitry_dreko
  • 451
  • 1
  • 5
  • 14

1 Answers1

0
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

struct values{
    vector<string> ve;
}array[100];

bool comp(values a, values b){
    if(a.ve.size()<b.ve.size() || a.ve<b.ve)
        return true;
    return false;
}

int main(){

    int number, size;
    string str;
    cin>>number; //Number of string arrays
    for(int i=0;i<number;i++){
        cin>>size; // Number of element in each array
        for(int j= 0;j<size;j++){
            cin>>str;
            array[i].ve.push_back(str);
        }

    }
    sort(array, array+number, comp);

    for(int i=0;i<number;i++){
        for(int j= 0;j<array[i].ve.size();j++){
            cout<<array[i].ve[j]<<" ";
        }
        cout<<"\n";

    }
    return 0;
}

Input:

3      
4
-22 -3 2.2 a
4
-22 -3 2.2 b
3
-22 0 2.2

Output:

-22 0 2.2 
-22 -3 2.2 a 
-22 -3 2.2 b
Masum
  • 401
  • 3
  • 14