I'm looking for some advice on a good way to achieve my goal. I feel like my pseudo code below is redundant and that there must be a more efficient way of doing it. My question is there a better solutioj to this or more efficcient way? So here is the setup...
I have a class called Node which has two properties
class Node
{
bool favorite
string name
}
I have a list which contains around a thousand of these Nodes. I want to give users three features..
A way to filter the list to just show favorites otherwise if favorites is false it displays the original list
Ability to search by string/name comparison
The ability for both the search and favorite to work in combination
below is my pseudo code - describes an approach, not ideal though. You can read the comments in the code to get the main gist.
// initial collection of nodes
list<Nodes> initialnodesList = [];
// list of nodes which are displayed in UI
list<Nodes> displayNodes = [];
public void FilterNodes()
{
list<Nodes> tempNodesList = [];
if (favoritesEnabled)
{
// collect favorites
foreach (n in initialnodesList)
if (n.favorite)
tempNodesList.add(n);
// search within favorites if needed and create new list
list<Nodes> searchedNodesList = [];
if (!isStringNullWhiteSpace(searchString))
{
foreach (n in tempNodesList)
if (n.name == searchString)
searchedNodesList.add(n);
displayNodes = searchedNodesList;
return;
}else{
return;
}
}
else
{
// search within initial node collection if needed and create new list
list<Nodes> searchedNodesList = [];
if (!isStringNullWhiteSpace(searchString))
{
foreach (n in initialnodesList)
if (n.name == searchString)
searchedNodesList.add(n);
displayNodes = searchedNodesList;
return;
}
// if search is not needed and favorites were not enabled then just return the original node collection
displayNodes = initialnodesList;
return;
}
}

Linq, especiallyIEnumerable.Where