2d array

Can I control only the row in a 2d array? I am trying to use flat sorting in a 2d array, where I should sort the array descendingly according to one particular column
For instance,

A B 12
A L 13
A p 14

it should be

A P 14
A L 13
A B 12

Any help?
Hello Jack Van Stone,

There is no easy way to sort a 2D array. You will need to write your own function to do this.

You could use a single for loop to control the "row" and hard code the column, but I feel that you will have to hard code both the row and column to do what you want.

This may be of some use to you http://www.cplusplus.com/forum/beginner/250170/#msg1101749 It will need to be adjusted, but may help.

Hope that helps,

Andy
arraytype * tp[numrows];
for(numrows…)
tp[I] = array[I];



sorting loop

if(tp[I][1] > tp[I+1][1]) //whatever logic, pretend this means swap the rows
swap(tp[I], tp[I+1]);

etc

then copy tp into a new array if you need an actual sorted array, or use tp directly if you can get by with a mask to your original (still unsorted) array. This single copy is going to save you tons vs copying a row at a time with a conventional method.


its really easy to get std::sort to do this for you using this idea too.
Last edited on
This isn't very generic, and isn't a 2D array, but might help.

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

struct Row {
    
    std::string name;
    std::string sign;
    int age;
    
    struct ColumnSorter {
        
        int column;
        
        ColumnSorter(int column)
        : column(column)
        { }
        
        /// > (greater than) for descending order
        /// < (less than) for ascending order
        /// use templates, if-statements, or virtual methods to customize further
        bool operator() (const Row& row1, const Row& row2)
        {
            if (column == 0)
            {
                return (row1.name > row2.name);
            }
            else if (column == 1)
            {
                return (row1.sign > row2.sign);
            }
            else if (column == 2)
            {
                return (row1.age > row2.age);
            }
            else
            {
                return false;   
            }
        }
    };
};

std::ostream& operator<<(std::ostream& os, const std::vector<Row>& rows)
{
    for (size_t i = 0; i < rows.size(); i++)
    {
        os  << rows[i].name << " " << rows[i].sign << " " << rows[i].age << '\n';
    }
    return os;
}

template <typename Collection, typename SortingPredicate>
void sort(Collection& collection, const SortingPredicate& method)
{
    std::sort(collection.begin(), collection.end(), method);
}

int main()
{
    std::vector<Row> rows {
        { "Billy",   "Cancer", 22 },
        { "Morgan",  "Gemini", 11 },
        { "Wyatt",   "Aries",  44 }
    };
    
    
    std::cout << rows << '\n';
    
    sort(rows, Row::ColumnSorter(0));
    std::cout << rows << '\n';
    
    sort(rows, Row::ColumnSorter(1));
    std::cout << rows << '\n';
    
    sort(rows, Row::ColumnSorter(2));
    std::cout << rows << '\n';
}
Last edited on
Topic archived. No new replies allowed.