code_junkie

How do I sort a CArray of a user defined type?

Nov 11th, 2011
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.51 KB | None | 0 0
  1. CArray<int> arrayOfInts;
  2. arrayOfInts.Add(7);
  3. arrayOfInts.Add(114);
  4. arrayOfInts.Add(3);
  5. std::sort(arrayOfInts.GetData(), arrayOfInts.GetData()+arrayOfInts.GetSize());
  6.  
  7. struct Foo
  8. {
  9. int val;
  10. double priority;
  11. };
  12.  
  13. bool FooPred(const Foo& first, const Foo& second)
  14. {
  15. if ( first.val < second.val )
  16. return true;
  17. if ( first.val > second.val )
  18. return false;
  19. return first.priority < second.priority;
  20. }
  21.  
  22. //...
  23.  
  24. CArray<Foo> bar;
  25. std::sort(bar.GetData(), bar.GetData()+bar.GetSize(), FooPred);
Add Comment
Please, Sign In to add comment