title: visual c++ Sort List of Custom Class
body: <p>I have a class student, defined in <code>class_student.h</code> like this:</p>
  
  <pre><code>#pragma once
  using namespace System;
  using System::Windows::Forms::MessageBox;
  using namespace System::Collections::Generic;
  using namespace MySql::Data::MySqlClient;
  
  public ref class student {
  
  public:
      String^ user_name;
      String^ my_class;
      int weighted_grades;
      int weighted_totals;
      float average_percent;
  
      int get_weighted_grades() {
          String^ con_string = L"datasource=127.0.0.1;port=3306;username=root;password=1234;database=comp4;";
          MySqlConnection^ con_database = gcnew MySqlConnection(con_string);
          MySqlCommand^ get_grades = gcnew MySqlCommand("select coalesce(sum(method_marks) + sum(accuracy_marks), 0) from tbl_grades inner join tbl_test_instances on tbl_grades.test_instance=tbl_test_instances.id inner join tbl_students on tbl_test_instances.student_id = tbl_students.username inner join tbl_tests on tbl_test_instances.test_id=tbl_tests.test_id where tbl_students.class = tbl_tests.class and username='" + user_name + "';", con_database);
          MySqlDataReader^ my_reader;
          int grades;
          try {
              con_database-&gt;Open();
              my_reader = get_grades-&gt;ExecuteReader();
              my_reader-&gt;Read();
              grades = my_reader-&gt;GetInt32(0);
          }
          catch (Exception^ ex) {
              MessageBox::Show(ex-&gt;Message);
          }
          return grades;
      }
  
      int get_weighted_totals() {
          String^ con_string = L"datasource=127.0.0.1;port=3306;username=root;password=1234;database=comp4;";
          MySqlConnection^ con_database = gcnew MySqlConnection(con_string);
          MySqlCommand^ get_totals = gcnew MySqlCommand("select coalesce(sum(method_marks) + sum(accuracy_marks), 0) from tbl_questions inner join tbl_test_questions on tbl_questions.question_id=tbl_test_questions.question_id inner join tbl_test_instances on tbl_test_questions.test_id=tbl_test_instances.test_id inner join tbl_students on tbl_test_instances.student_id=tbl_students.username inner join tbl_tests on tbl_test_instances.test_id=tbl_test_questions.test_id where tbl_students.class=tbl_tests.class and username='" + user_name + "';", con_database);
          MySqlDataReader^ my_reader;
          int totals;
          try {
              con_database-&gt;Open();
              my_reader = get_totals-&gt;ExecuteReader();
              my_reader-&gt;Read();
              totals = my_reader-&gt;GetInt32(0);
          }
          catch (Exception^ ex) {
              MessageBox::Show(ex-&gt;Message);
          }
          return totals;
      }
  
      student(String^ user_name, String^ txt_class) {
          this-&gt;user_name = user_name;
          this-&gt;my_class = txt_class;
          this-&gt;weighted_grades = get_weighted_grades();
          this-&gt;weighted_totals = get_weighted_totals();
          this-&gt;average_percent = ((float)weighted_grades / (float)weighted_totals) * 100;
      }
  
  };
  </code></pre>
  
  <p>I have a List <code>List&lt;student^&gt;^ students = gcnew List&lt;student^&gt;();</code> defined in another header file (with the class file included). This list has several instances of the class student added to it.</p>
  
  <p>I would like to use <code>students-&gt;Sort()</code> or something of the like to sort through this list. I tried overwriting the <code>operator&lt;</code>, both inside the class definition and outside it, but when I call <code>Sort()</code> I still get an error message saying that the items cannot be compared.</p>
  
  <p>When trying to do this through the <code>operator&lt;</code>, I used this, if that helps:</p>
  
  <pre><code>#pragma once
  using namespace System;
  using System::Windows::Forms::MessageBox;
  using namespace System::Collections::Generic;
  using namespace MySql::Data::MySqlClient;
  
  public ref class student {
  
  public:
      String^ user_name;
      String^ my_class;
      int weighted_grades;
      int weighted_totals;
      float average_percent;
  
      int get_weighted_grades() {
          String^ con_string = L"datasource=127.0.0.1;port=3306;username=root;password=1234;database=comp4;";
          MySqlConnection^ con_database = gcnew MySqlConnection(con_string);
          MySqlCommand^ get_grades = gcnew MySqlCommand("select coalesce(sum(method_marks) + sum(accuracy_marks), 0) from tbl_grades inner join tbl_test_instances on tbl_grades.test_instance=tbl_test_instances.id inner join tbl_students on tbl_test_instances.student_id = tbl_students.username inner join tbl_tests on tbl_test_instances.test_id=tbl_tests.test_id where tbl_students.class = tbl_tests.class and username='" + user_name + "';", con_database);
          MySqlDataReader^ my_reader;
          int grades;
          try {
              con_database-&gt;Open();
              my_reader = get_grades-&gt;ExecuteReader();
              my_reader-&gt;Read();
              grades = my_reader-&gt;GetInt32(0);
          }
          catch (Exception^ ex) {
              MessageBox::Show(ex-&gt;Message);
          }
          return grades;
      }
  
      int get_weighted_totals() {
          String^ con_string = L"datasource=127.0.0.1;port=3306;username=root;password=1234;database=comp4;";
          MySqlConnection^ con_database = gcnew MySqlConnection(con_string);
          MySqlCommand^ get_totals = gcnew MySqlCommand("select coalesce(sum(method_marks) + sum(accuracy_marks), 0) from tbl_questions inner join tbl_test_questions on tbl_questions.question_id=tbl_test_questions.question_id inner join tbl_test_instances on tbl_test_questions.test_id=tbl_test_instances.test_id inner join tbl_students on tbl_test_instances.student_id=tbl_students.username inner join tbl_tests on tbl_test_instances.test_id=tbl_test_questions.test_id where tbl_students.class=tbl_tests.class and username='" + user_name + "';", con_database);
          MySqlDataReader^ my_reader;
          int totals;
          try {
              con_database-&gt;Open();
              my_reader = get_totals-&gt;ExecuteReader();
              my_reader-&gt;Read();
              totals = my_reader-&gt;GetInt32(0);
          }
          catch (Exception^ ex) {
              MessageBox::Show(ex-&gt;Message);
          }
          return totals;
      }
  
      student(String^ user_name, String^ txt_class) {
          this-&gt;user_name = user_name;
          this-&gt;my_class = txt_class;
          this-&gt;weighted_grades = get_weighted_grades();
          this-&gt;weighted_totals = get_weighted_totals();
          this-&gt;average_percent = ((float)weighted_grades / (float)weighted_totals) * 100;
      }
      bool operator&lt;(student^ other) {
          return this-&gt;average_percent &gt; other-&gt;average_percent;
      }
  };
  </code></pre>
  
  <p>Or this:</p>
  
  <p>class_student.h:</p>
  
  <pre><code>[class definition as shown at the top]
  
  bool operator&lt;(student^ s1, student^ s2);
  </code></pre>
  
  <p>class_student.cpp:</p>
  
  <pre><code>#include "class_student.h"
  
  bool operator&lt;(student^ s1, student^ s2) {
      return s1-&gt;average_percent &gt; s2-&gt;average_percent;
  }
  </code></pre>