3

I have a list of users that I want to order by an ID. So now the list is ordered by a users fullname like this:

OrderBy(m => m.FullName).ToList();

What I want to do is order this list so a specific id is set to be at the start index.

So the pseudo code would be:

var passedId = 2123

OrderBy(m=>m.ID); // And then set passedId to be in the start index of the list

Is this possible ?

Thanks.

Red
  • 2,728
  • 1
  • 20
  • 22
Lahib
  • 1,305
  • 5
  • 34
  • 62
  • 3
    Can you show some sample input \ output? Hard to understand what you are asking. – Rahul Singh Dec 20 '15 at 10:42
  • i dont understand what it is you need to see. The list contains User properties. One of these propperties is an ID. I wan to pass and ID to the controller and then order my list with this ID as the start index. – Lahib Dec 20 '15 at 10:47
  • Unclear what your wanting, but best guess - `.Where(m => m.ID >= thisID).OrderBy(m => m.ID)` –  Dec 20 '15 at 10:47

3 Answers3

2

You can do this.

var passedId = 2123;
list = list.OrderByDescending(m => m.Id == passedId).ThenBy(m => m.FullName).ToList();

This makes the user with specific id to be first and others will be ordered by name.

Or if the list is already ordered by name you can do this.

var user = list.FirstOrDefault(m => m.Id == passedId); // find user with id
if(user != null)
{
    list.Remove(user); // remove user
    list.Insert(0, user); // add user to beginning.
}
M.kazem Akhgary
  • 18,645
  • 8
  • 57
  • 118
  • for some reason i cannot user the first example you suggested. It still orders the users by fullname. The second example i cant use because i cant use lambda expressions in indexOf because it takes a User as argument – Lahib Dec 20 '15 at 11:25
  • @Lahib ive edited the second code. it should work now. btw for the first code try changing it to `list.OrderBy(m => m.FullName).ThenByDescending(m => m.Id == passedId)` – M.kazem Akhgary Dec 20 '15 at 11:47
  • i thank you for the help i appreciate it, – Lahib Dec 20 '15 at 12:06
2

You can make use of the OrderBy and ThenBy expressions provided by LINQ.

The first and most important rule at the moment of ordering is that the object matches a specified ID as you said, so the OrderBy should use that as the condition. The second rule is to be ordered by name, so you should use the ThenBy with the m.FullName condition.

var orderedList = list.OrderBy(m => m.ID == specifiedId).ThenBy(m => m.FullName).ToList();

Here's another question on stackoverflow which has a nice and clear answer adressing OrderBy and ThenBy :

https://stackoverflow.com/a/3760014/5698997

Community
  • 1
  • 1
Snak
  • 673
  • 1
  • 5
  • 19
  • here `OrderBy` moves the true items at the end. because the ascending order is `false -> true`. you should use descending order i.e `true -> false` – M.kazem Akhgary Dec 20 '15 at 11:12
  • i tried doing this. But for some reason it still takes fullname ordering. Maybe it has something to do with the list is also ordered somewhere else or does that not matter ? – Lahib Dec 20 '15 at 11:33
2

I personally don't like relying on bool ordering (looks strange and not natural) and prefer using an explicit expression. In your case, something like this

// ...
.OrderBy(m => m.ID == passedId ? 0 : 1)
.ThenBy(m => m.FullName) // or whatever is needed

Even if the list is already ordered by some unknown criteria, you can still apply OrderBy (w/o ThenBy) like above because according to the Enumerable.OrderBy documentation

This method performs a stable sort; that is, if the keys of two elements are equal, the order of the elements is preserved.

Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343