First of all, it’s completely unclear why you allocate memory dynamically when the size of your array is constant.
If the size of the array can change depending on the number of lines in the file, it is better to use the standard class std::vector .
You can declare a simple structure that will store the selected year, month, and day values for a given date.
You can also immediately sort the vector by increasing dates.
The program is shown below as well. Only instead of the input file, I used a stream of lines for simplicity in the program.
#include <iostream> #include <vector> #include <string> #include <sstream> #include <algorithm> #include <functional> struct Date { unsigned int year; unsigned int month; unsigned int day; }; int main() { std::string s = "15.11.2016\n" "30.12.1998\n"; std::vector<Date> dates; std::istringstream input( s ); std::string line; while ( std::getline( input, line ) ) { Date d = {}; char c; std::istringstream is( line ); is >> d.day >> c >> d.month >> c >> d.year; dates.push_back( d ); } std::sort( dates.begin(), dates.end(), []( const Date &d1, const Date &d2 ) { return std::tie( d1.year, d1.month, d1.day ) < std::tie( d2.year, d2.month, d2.day ); } ); for ( Date d : dates ) { std::cout << d.day << '.' << d.month << '.' << d.year << std::endl; } return 0; }
Output of the program to the console
30.12.1998 15.11.2016
To compare dates stored in structure objects, you can define operator < as follows
bool operator <( const Date &d1, const Date d2 ) { return std::tie( d1.year, d1.month, d1.day ) < std::tie( d2.year, d2.month, d2.day ); }