Richard77
Richard77

Reputation: 21641

How to move a range of items in a list to the end of the list

I want to display user client note at the bottom of the screen. What I'm doing is to separate user client note from the rest of the notes then add them again to the list. Since AddRange add the range at the end, so I obtain what I expected.

var note1 = _notes.Where(n => n.NoteTypeID != (int)NoteTypes.User_Client_Note);
var note2 = _notes.Where(n => n.NoteTypeID == (int)NoteTypes.User_Client_Note);
_notes = new List<ProjectsActiveNote>();
_notes.AddRange(note1);
_notes.AddRange(note2);

I wonder whether there is a method that does this directly.

Upvotes: 1

Views: 267

Answers (2)

dontangg
dontangg

Reputation: 4809

You could use the OrderBy.

_notes = _notes.OrderBy(n => n.NoteTypeID != (int)NoteTypes.User_Client_Note ? 0 : 1).ToList();

Essentially, it will put the notes that aren't user client notes first because the order by will return 0 for those and it will return 1 for the others. In my test, it still preserves the order within the separate groups.

Upvotes: 2

BradleyDotNET
BradleyDotNET

Reputation: 61349

There is no way to do a "group move" like that in a list.

However, you can write a query that returns the elements in the correct order, and even enumerate them and store them in a list if you wanted to.

var clientNotes = _notes.Where(n => n.NoteTypeID == (int)NoteTypes.User_Client_Note);
return notes.Execpt(clientNotes).Concat(clientNotes);

Will return an IEnumerable with the order how you wanted it.

Upvotes: 0

Related Questions