I have the following 2d array that I get/create from an external .txt file.
string accountsArr[5][7] = {
"[email protected]", "Blake", "Ham", "squid62", "1987", "U", "Teacher",
"[email protected]", "Jim", "Dark", "gymrat32", "1985", "A", "Master",
"[email protected]", "Hannah", "Green", "flower22", "2007", "U", "Apprentice",
"[email protected]", "Tom", "Smith", "tuna20", "2000", "U", "Teacher",
"[email protected]", "James", "Arrow", "ahoy10", "2005", "U", "Apprentice"
};
I need to sort this array based on the "last name" column (column index 2 for each row), so I basically end up with:
string accountsArr[5][7] = {
"[email protected]", "James", "Arrow", "ahoy10", "2005", "U", "Apprentice",
"[email protected]", "Jim", "Dark", "gymrat32", "1985", "A", "Master",
"[email protected]", "Hannah", "Green", "flower22", "2007", "U", "Apprentice",
"[email protected]", "Blake", "Ham", "squid62", "1987", "U", "Teacher",
"[email protected]", "Tom", "Smith", "tuna20", "2000", "U", "Teacher"
};
How would I do this, programmatically? Using std::sort isn't working. I keep getting use of undeclared identifier sort.
int n = sizeof(accountsArr[0]) / sizeof(accountsArr[0][0]);
std::sort(accountsArr, accountsArr + n);
UPDATE: I need/want to know how to do this on a primitive string array specifically (no vectors, structs, etc).
std::sort? Do you know how to use it, in C++? Are there examples ofstd::sortin your C++ textbook that you can look at? Is there anything specific about sorting that you're unsure about?structfor each of these fields and then overloadoperator <?std::sortworks, and then figuring out how to make it work with your arrays. There is no instant gratification in C++.