title: Sorting struct-members by a specific value
body: <p>I've had this problem for a while now so I thought it would be better to ask for direction. Tried search with no help and been struggling with this pretty long now so here we go.</p>
  
  <p>I'm learning C++ and got hard time on this exercise: I have to save details of 3 students using struct and then print them in ascending order by their distance from school. First student's details are asked by user input and others are initialized by variables. Everything goes fine untill I have to sort them out. </p>
  
  <p>At the moment I'm trying to pass an array to a sorting function, containing each students details which I entered manually to that array for testing purposes. I had some serious problems when I tried to initialize array directly from struct member values and then passing that array to sorting func. Also, in this exercise I must use C-arrays.</p>
  
  <p>Perpaps someone can show me proper way of doing this? Maybe there is even easier way to do this? Thanks in advance! Here is my code:</p>
  
  <p>EDIT: Platform: Windows 7, IDE: Visual Studio 2013</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;algorithm&gt;
  #include &lt;functional&gt;
  
  using namespace std;
  
  struct personaldata {
      char firstname[50], lastname[50], address[50], postal[50];
      double distschool;
      int shoesize;
  };
  
  struct SortRecordsAscending
  {
      bool operator()(const personaldata&amp; lhs, const personaldata&amp; rhs)
      {
          return (lhs.distschool &lt; rhs.distschool);
      }
  };
  
  void SortingObjectsExample(personaldata *details, int SIZE)
  {
      sort(details, details + SIZE, SortRecordsAscending());
      for (int i = 0; i &lt; SIZE; ++i)
      {
          cout &lt;&lt; details[i].firstname &lt;&lt; " " &lt;&lt; details[i].lastname &lt;&lt; endl;
          cout &lt;&lt; details[i].address &lt;&lt; " " &lt;&lt; details[i].postal &lt;&lt; endl;
          cout &lt;&lt; "Distance from school: " &lt;&lt; details[i].distschool &lt;&lt; endl;
          cout &lt;&lt; "Shoe size: " &lt;&lt; details[i].shoesize &lt;&lt; endl;
          cout &lt;&lt; endl;
      }
  }
  
  int main() {
  
      personaldata student1, student2, student3;
      const int SIZE(3); // Amount of students
  
  
      //Student 1
      cout &lt;&lt; "Enter first name: ";
      cin.getline(student1.firstname, 50);
  
      cout &lt;&lt; "Enter last name: ";
      cin.getline(student1.lastname, 50);
  
      cout &lt;&lt; "Enter distance from school: ";
      cin &gt;&gt; student1.distschool;
      cin.ignore();
  
      cout &lt;&lt; "Enter address: ";
      cin.getline(student1.address, 50);
  
      cout &lt;&lt; "Enter postal: ";
      cin &gt;&gt; student1.postal;
  
      cout &lt;&lt; "Enter shoe size: ";
      cin &gt;&gt; student1.shoesize;
  
      //Student 2
      strcpy_s(student2.firstname, 50, "Olli");
      strcpy_s(student2.lastname, 50, "Oppilas");
      student2.distschool = 9.4;
      strcpy_s(student2.address, 50, "Opiskelijakatu 5a 14");
      strcpy_s(student2.postal, 50, "40200");
      student2.shoesize = 39;
  
      //Student 3
      strcpy_s(student3.firstname, 50, "Ossi");
      strcpy_s(student3.lastname, 50, "Oppilas");
      student3.distschool = 8.4;
      strcpy_s(student3.address, 50, "Koululaiskatu 18b 2");
      strcpy_s(student3.postal, 50, "40100");
      student3.shoesize = 41;
  
      personaldata details[3] = {
              { "Oiva", "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
              { "Ossi", "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
              { "Olli", "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
      };
  
      SortingObjectsExample(details, SIZE);
  
  }
  </code></pre>
  
  <p>Code above works the way I want it to. However if I do like this:</p>
  
  <pre><code>personaldata details[3] = {
          { student1.firstname, "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
          { student2.firstname, "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
          { student3.firstname, "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
  };
  
  SortingObjectsExample(details, SIZE);
  </code></pre>
  
  <p>I get from compiler</p>
  
  <pre><code>1&gt;------ Build started: Project: Harj17, Configuration: Debug Win32 ------
  1&gt;  harj17.cpp
  1&gt;harj17.cpp(80): error C2440: 'initializing' : cannot convert from 'char (*)[50]' to 'char'
  1&gt;There is no context in which this conversion is possible
  1&gt;harj17.cpp(80): warning C4244: 'initializing' : conversion from 'double' to 'char', possible loss of data
  1&gt;harj17.cpp(80): error C2440: 'initializing' : cannot convert from 'char [50]' to 'char'
  1&gt;          There is no context in which this conversion is possible
  ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>And if use pointer like this:</p>
  
  <pre><code>    personaldata details[3] = {
          { *student1.firstname, "Oppilas", "Koivukatu 8B 16", "40100", 9.7, 43 },
          { *student2.firstname, "Oppilas", "Koululaiskatu 18b 2", "40100", 8.4, 41 },
          { *student3.firstname, "Oppilas", "Lehtitie 3B 15", "401200", 8.7, 38 }
  };
  </code></pre>
  
  <p>It will compile the program but with warning message and it doesn't work properly, it only prints first letter of person's first name.</p>
  
  <pre><code>    1&gt;------ Build started: Project: Harj17, Configuration: Debug Win32 ------
  1&gt;  harj17.cpp
  1&gt;harj17.cpp(80): warning C4244: 'initializing' : conversion from 'double' to 'char', possible loss of data
  1&gt;  Harj17.vcxproj -&gt; Harj17.exe
  ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
  </code></pre>
  
  <p>Here is the output when running the program. You can see problem with the names and other info gets screwed up too.</p>
  
  <pre><code>Enter first name: Example
  Enter last name: Student
  Enter distance from school: 13.37
  Enter address: Example Address 6B 16
  Enter postal: 1337
  Enter shoe size: 43
  EOppilas Koivukatu 8B 16
  40100   +
  Distance from school: 0
  Shoe size: 0
  
  OOppilas Koululaiskatu 18b 2
  40100)
  Distance from school: 0
  Shoe size: 0
  
  OOppilas Lehtitie 3B 15
  401200&amp;
  Distance from school: 0
  Shoe size: 0
  </code></pre>