I have a class called Task
that contain a member boolean variable Completed
.
I created a list of Task
objects in a variable called Tasks
.
I have the following code, which sorts the objects depending on whether the Task is completed. (I want the incompleted tasks to be in the list first)
private List<Task> GetSortedTask(List<Task> tasks)
{
List<Task> completedTaskList = new List<Task>();
List<Task> incompleteTaskList = new List<Task>();
for (int i = 0; i < tasks.Count; i++)
{
HelperObject T = task[i].getHelperObject();
if (T != null && T.Completed)
completedTaskList.Add(tasks[i]);
else
incompletetaskList.Add(tasks[i]);
}
//merge the two lists together
imcompleteTaskList.AddRange(completedTaskList);
return incompleteTaskList;
}
Is there a way to do this without creating so many lists? Thanks in advance
EDIT: Made my question a bit more specific since orderBy
seems to be the correct answer, however my problem may be a bit more complicated than that.
IQueryable
, so long as the data structure containing it (List
) does.tasks.OrderBy(p => p.Completed)
should be perfectly sufficient, unless you want the other direction, in which case you can usetasks.OrderByDescending(p => p.Completed)
. \$\endgroup\$