sorting - cli C++ sort a objectlist on a certain property -


i want sort list on property.

i've got linepieceobject following properties:

string^ type; int x, y, x2, y2; system::string^ text; 

now have list these linepieces , want sort them on x value.

i found in list->sort(); need give information. don't know how tell sort list on x value.

so how can sort list on x value of object?

if read between lines of question, sounds want sort based on x value, , want sort based on y value. if case, i'd implement comparer object, , pass list->sort() specify how should sorted.

public ref class comparebyx : comparer<linepiece^> { public:     virtual int compare(linepiece^ a,linepiece^ b) override     {         return a->x.compareto(b->x);     } };  int main(array<system::string ^> ^args) {     list<linepiece^>^ list = ...      list->sort(gcnew comparebyx()); } 

on other hand, if linepiece has single, innate, universal sorting order, i'd implement icomparable on class, , use default sorting. however, when this, should careful return 0 when 2 objects equal.

when this, don't need pass parameters sort(), since objects know how sort themselves.

public ref class linepiece : public icomparable<linepiece^> { public:     string^ type;     int x, y, x2, y2;     string^ text;      virtual int compareto(linepiece^ other)     {         int result = 0;          if (result == 0) result = this->x.compareto(other->x);         if (result == 0) result = this->y.compareto(other->y);         if (result == 0) result = this->x2.compareto(other->x2);         if (result == 0) result = this->y2.compareto(other->y2);         if (result == 0) result = this->type->compareto(other->type);         if (result == 0) result = this->text->compareto(other->text);          return result;     } }  int main(array<system::string ^> ^args) {     list<linepiece^>^ list = ...      list->sort(); } 

Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -