简体   繁体   中英

How To Sort List In Ascending Order After AddRange

Here I am trying to sort list in Ascending Order after AddRange , I have a list where at 0 index ie [0] there is data with MessageId=20 and at 1 index ie [1] there is data with MessageId=19 .

Now what I want is to sort the list in ascending order based on that MessageId .For this I have used OrderBy(x => x.MessageId) .
But the problem is that even after using OrderBy(x => x.MessageId) list is not sorted in ascending order basedon MessageId.

Any help will be grate.Thank you

Below is what I have done to sort

messagesList.AddRange(MsgList);
messagesList.OrderBy(x => x.MessageId).ToList();

ToList() Method Return List; you need Set result of Ordering. like this:

messagesList = messagesList.OrderBy(x => x.MessageId).ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM