Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to sort a 2d array in c++

bool sortcol(const vector<int>& v1, const vector<int>& v2)
{
    return v1[1] < v2[1];
}
sort(vect.begin(), vect.end(), sortcol);
Comment

order 2d array in c++

9

The built-in arrays of C and C++ are very inflexible, among other things they cannot be assigned.

Your best option would be the 'array' class from the C++ standard library, at least for the inner dimension:

array<int, 2> a[5] = { { 20, 11 },
{ 10, 20 },
{ 39, 14 },
{ 29, 15 },
{ 22, 23 } };

sort( a, a + 5 );
Edit: Some more explanations.
Here we use the property of std::array that '<' by default compares them lexicographically, i.e. starts with the first element. In order to sort things differently we have to come up with an comparator object, so if you want to use the second column as sort key you have to do this:

auto comp = []( const array<int, 2>& u, const array<int, 2>& v )
      { return u[1] < v[1]; };
sort( a, a + 5, comp );
And as mentioned in the first comment, sort(a, a+5 ... is just an ugly short form for the cleaner sort(std::begin(a), std::end(a) ...
Comment

sort 2d vector c++

std::sort(vector1.begin(),
          vector1.end(),
          [] (const std::vector<double> &a, const std::vector<double> &b)
          {
              return a[3] < b[3];
          });
Comment

sort 2d array

function sort_array() {
  
notice_board_data.sort(function (a, b) {
    return Date.parse(a[0].localeCompare(b[0])) || a[1].localeCompare(b[1]);
});

}//end function
Comment

PREVIOUS NEXT
Code Example
Cpp :: new in c++ 
Cpp :: dangling pointer in cpp 
Cpp :: count c++ 
Cpp :: c++ function pointer 
Cpp :: c++ concatenate strings 
Cpp :: c++ length of int 
Cpp :: how to create a struct in c++ 
Cpp :: c++ check first character of string 
Cpp :: know what the input data is whether integer or not 
Cpp :: sort an array in c++ 
Cpp :: c++ last element of vector 
Cpp :: error in c++ 
Cpp :: c++ stl 
Cpp :: Check whether the jth object is in the subset 
Cpp :: fname from FString 
Cpp :: recuva recovery software for pc with crack 
Cpp :: Write a C++ program to Computing Mean and Median Using Arrays 
Cpp :: c++ file handiling 
Cpp :: convert hex to decimal arduino 
Cpp :: find n unique integers sum up to zero 
Cpp :: nand in cpp 
Cpp :: C++ for vs while loops 
Cpp :: is obje file binary?? 
Cpp :: +905344253752 
Cpp :: what do I return in int main() function c++ 
Cpp :: powershell script query mssql windows authentication 
Cpp :: Your age doubled is: xx where x is the users age doubled. (print answer with no decimal places) 
Cpp :: set the jth bit from 1 to 0 
Cpp :: free pair c++ 
Cpp :: convert c program to c ++ online 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =