question

Kavithma-4766 avatar image
1 Vote"
Kavithma-4766 asked Kavithma-4766 commented

How to sort my List

HI all,
I have a below list

var listItems = new List<Item>
{
new Item() { OrderValue= 100, Balance= 90},
new Item() { OrderValue= 100, Balance= 70},
new Item() { OrderValue= 100, Balance= 0},
new Item() { OrderValue= 6, Balance= 0},
};

I need to sort the above list and get the below results

OrderValue= 100, Balance= 90 // 1st as OrderValue - Balance Difference is =10 and this value is high priority in asc order in the list
OrderValue= 100, Balance= 80 // 2nd as OrderValue - Balance Difference is =20 and this value is high priority in asc order in the list
OrderValue= 6, Balance= 0 // 3rd as OrderValue - Balance Difference is =6 but this willd not be prioratised first in the list becasue of the OrderValue is less compared to the two other list items above
OrderValue= 100, Balance= 0 // 4th as OrderValue - Balance Difference is = 100 and this will be the last priority as highst value for this should be at the last.


so how do we sort my list to get the above reslts ?

dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MaheswariSivanandam-1024 avatar image
0 Votes"
MaheswariSivanandam-1024 answered Kavithma-4766 commented

just add SortingField in your Item class

public class Item
{
public int OrderValue { get; set; }
public int Balance { get; set; }
public int SortingField {
get
{
return this.OrderValue - this.Balance;
}
}

}

then use the following coding to meet your sorting needs,

var sortedList = listItems.OrderByDescending(q => q.Balance).ThenBy(q=>q.SortingField).ToList();

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for the help looks like it's working : )

0 Votes 0 ·
MaheswariSivanandam-1024 avatar image
0 Votes"
MaheswariSivanandam-1024 answered Kavithma-4766 commented

use the following code to sort the list based on id property,
var sortedList = listItems.OrderBy(q => q.id).ToList();

· 6
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi Maheshwari , sorry I inititially added the id column to explain my question to the communicty, but I have removed the id column as in my actual list I dont have the Id colum , how do I do this without the id column ?

0 Votes 0 ·

ok, in you code is the OrderValue max value always 100?

0 Votes 0 ·
Kavithma-4766 avatar image Kavithma-4766 MaheswariSivanandam-1024 ·

no it could be anything

0 Votes 0 ·
Show more comments
Kavithma-4766 avatar image
0 Votes"
Kavithma-4766 answered

Thanks for helping me this , I will test this and update you :)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.