-2

How can i sort down this struct only with the int ?

   struct buch {         
   string buchtitel;
   int preis;
   } buch;
Rene B
  • 1
  • 2
  • You need more explaination about your issue, sorting a lonely struct is not something that makes sense. Maybe you forgot avout a vector or a list of `buch` to be sorted ? – Lohrun Oct 31 '13 at 09:59
  • Why have you created an instance of buch also called buch? – CashCow Oct 31 '13 at 10:02
  • possible duplicate of [How to sort an array of structs in C?](http://stackoverflow.com/questions/8721189/how-to-sort-an-array-of-structs-in-c) – hyde Oct 31 '13 at 10:02
  • Yet again: are you asking about C or C++? They are different languages, with different libraries offering different ways to sort things. – Mike Seymour Oct 31 '13 at 10:12
  • I think by the user accepting my answer, it is obvious that I correctly determined what they were asking. – CashCow Oct 31 '13 at 12:52

1 Answers1

2

If this is C++11 you can use a lambda function.

std::sort
  ( 
     beginIter, endIter, 
     []( buch const& lhs, buch const& rhs ){ return lhs.preis < rhs.preis; } 
  );

where beginIter and endIter define random-access iterators to the items you wish to sort, endIter being one past the end of the range.

CashCow
  • 30,981
  • 5
  • 61
  • 92