fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5.  
  6. using std::cout;
  7. using std::cin;
  8. using std::endl;
  9. using std::string;
  10. using std::swap;
  11.  
  12. struct studentStruct {
  13. string firstName;
  14. //string lastName;
  15. //int grade;
  16. //float GPA;
  17. };
  18.  
  19. enum sortField {eFirstName, eLastName, eGrade, eGPA};
  20. enum sortDirection {eAscending, eDescending};
  21.  
  22. bool compData( studentStruct s1, studentStruct s2, sortField field, sortDirection direction)
  23. {
  24. switch(field)
  25. {
  26. case eFirstName:
  27. {
  28. string f1 = s1.firstName;
  29. string f2 = s2.firstName;
  30. switch(direction)
  31. {
  32. case eAscending:
  33. {
  34. if(f2 < f1)
  35. return true;
  36. else
  37. return false;
  38. }
  39. case eDescending:
  40. {
  41. if(f2 > f1)
  42. return true;
  43. else
  44. return false;
  45. }
  46. }
  47. }
  48. }
  49. }
  50.  
  51. void sort( studentStruct s[], enum sortField field, int length, sortDirection d)
  52. {
  53. for(int i = 0; i < length - 1; i++)
  54. {
  55. for(int j = 0; j < length - 1 - i; j++)
  56. {
  57. if(compData(s[j], s[j+1], field, d) == true)
  58. {
  59. swap(s[j], s[j+1]);
  60. cout << "SWAP" << endl;
  61. }
  62. }
  63. }
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69. struct studentStruct s[5] = {{"M"}, {"I"}, {"K"}, {"O"}, {"N"}};
  70. sort(s, eFirstName, 5, eAscending);
  71. for(int i = 0; i < 5; i++)
  72. cout << s[i].firstName << endl;
  73. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
SWAP
SWAP
SWAP
I
K
M
N
O