HRusev

Sort Pointers

Jul 28th, 2023
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | Source Code | 0 0
  1. #ifndef SORT_BY_H
  2. #define SORT_BY_H
  3.  
  4. #include <algorithm>
  5.  
  6. #include "Company.h"
  7.  
  8.  
  9. void sortBy(Company** frontPtrs, Company** endPtrs, bool(&lessThan)(const Company& a, const Company& b))
  10. {
  11.     Company* temp;
  12.     bool flag;
  13.     int size = endPtrs - frontPtrs;
  14.     for(int i = 0; i < size - 1; i++)
  15.     {
  16.         flag = false;
  17.         for (int j = 0; j < size - i - 1; j++)
  18.         {
  19.             if (!lessThan(*frontPtrs[j], *frontPtrs[j + 1]))
  20.             {
  21.                 temp = frontPtrs[j];
  22.                 frontPtrs[j] = frontPtrs[j + 1];
  23.                 frontPtrs[j + 1] = temp;
  24.                 flag = true;
  25.             }
  26.         }
  27.         if (!flag)
  28.             break;
  29.     }
  30. }
  31.  
  32. #endif // !SORT_BY_H
Advertisement
Add Comment
Please, Sign In to add comment