# question

1 Vote"

## 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 ?

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

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

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 : )

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

· 6

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 ?

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

MaheswariSivanandam-1024

no it could be anything

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

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