How to sort struct by STL list containers

Source: Internet
Author: User

The list in the linked list has a custom sort sorting function, which is sorted from large to small by default. However, only the content of the numeric type that can be compared with the size can be used, such as int, float, and double, you need to define the sorting function for struct.

For example:

Define struct:

Typedef
StructNewtreeelem

{

LongNnodeid;
//Node ID

IntNlevel;
//Level

DoubleDsoin;
//Social impact

};

Defines global comparison functions,Note that it must be a global function.:

////Comparison

BoolCompinfo (newtreeelem first, newtreeelem second)

{

If(First. dsoin <= second. dsoin) // sort by size to size. // If You Want To increase by size, change to a value greater

{

Return
False;

}

Else

{

Return
True;

}

}

Define linked list:

List <newtreeelem> listsocialinf

Sort linked lists:

//Sort neighbors by social impact in descending order

Listsocialinf. Sort (compinfo );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.