There is an array of string M [21] [6]. Array You need to sort it by the last column while maintaining the binding to the row. The second day I try to do something, nothing happens.
It is desirable to have the solution as simple as possible, without using any complex functions.

2 answers 2

Something like this.

Such a solution does not pull on the “simplest”, but the code is a bit.

#include <algorithm> #include <array> #include <iomanip> #include <iostream> #include <string> #include <vector> int main() { std::vector<std::array<std::string, 3>> arr // Тут вместо 3 у вас будет 6. { {"a", "b", "12"}, {"e", "f", "28"}, {"c", "d", "14"}, {"foo", "bar", "6"}, {"hello", "world", "5"}, }; // Печатаем for (const auto &x : arr) { for (const auto &y : x) std::cout << std::setw(10) << y; std::cout << '\n'; } std::cout << '\n'; // Сортируем std::sort(arr.begin(), arr.end(), [](const auto &a, const auto &b) { int column = a.size()-1;// Номер столбца = последний const auto &a_str = a[column]; const auto &b_str = b[column]; if (auto x = a_str.size() - b_str.size()) return x > 0; return a_str < b_str; }); // Снова печатаем for (const auto &x : arr) { for (const auto &y : x) std::cout << std::setw(10) << y; std::cout << '\n'; } } 

Demo

  • one
    Thank you very much. I'm not sure that I can protect this solution, but I will understand, since there are no other options - Amelia Parker
  • one
    @AmeliaParker does not need to protect someone else's code, write your own. Everything is obvious there. So you never write anything useful yourself - jNX

@HolyBlackCat in its response has already given the direction of how to implement arbitrary sorting using the methods of the standard library. I recommend to consider in more detail how sort works, since You will need to write “your” sorting only when learning in order to understand how it works and “fill” the hand. In practice, it is almost always necessary to use standard methods for sorting: firstly, it is faster, secondly, there are less chances to make a mistake.

I will write in my answer how to apply the sorting algorithm to a two-dimensional array.

See, the main thing is the algorithm. How to apply it to a two-dimensional array / multidimensional array / vector / list is the implementation details. T.ch. if you want to implement sorting yourself, you need to start by choosing a sorting algorithm, there are a lot of them .

Suppose you have chosen the simplest option: sorting by inserts . We implement it for a simple case: a one-dimensional array. Something will turn out like:

 int n = 3; string m[] = { "17463", "19436", "16789" }; for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { if(m[i]>m[j]) { //сравнение string tmp = m[i]; //перестановка m[i] = m[j]; m[j] = tmp; } } } 

Run and make sure the algorithm works. If necessary, review how the array changes at each step, so that it is clear how the algorithm works.

Characteristically, the basic principle of the algorithm will remain the same for any kind of input data. Only technical moments will change: elements will be compared differently, otherwise they will be rearranged. These points are noted in the code above comments. For clarity, we will put them into separate functions:

 bool more(string a[], int i, int j) { return a[i] > a[j]; } void swap(string a[], int i, int j) { string tmp = a[i]; a[i] = a[j]; a[j] = tmp; } ... for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { if(more(m, i, j)) { //сравнение swap(m, i, j); } } } ... 

Now, in order to go to the case where m is a two-dimensional array, you only need to rewrite these two methods.

The comparison method is easily rewritten, just compare the last elements:

 bool more(string a[][6], int i, int j) { return a[i][5] > a[j][5]; } 

With permutation is more difficult. to copy the rows of the array you will need to go through all the columns of the array. Here is the blank, add the cycles yourself:

 void swap(string a[][6], int i, int j) { //временная переменная для хранения строки string tmp[6]; for(int x=0; x<6; x++) { //копируем a[i] в tmp } //a[i] = a[j]; //a[j] = tmp; } 

After that, the algorithm should work without significant changes:

 int n = 3; string m[][6] = { {"", "", "Evansville", "", "", "17463"}, {"", "", "Biloxi", "", "", "19436"}, {"", "", "London", "", "", "16789"} }; for(int i=0; i<n; i++) { for(int j=i+1; j<n; j++) { if(more(m, i, j)) { swap(m, i, j); } } } 

The functions can then be transferred back to the code, the main thing is that you understand the idea of ​​the algorithm. The code will be pretty simple anyway.

This approach can be applied to any sorting algorithm, the main thing is to note what technical features will change during the transition from one-dimensional to two-dimensional array.