Skip to content

Narwhal Simulator 15: Sorting and Filtering

Hi everyone! I’ve done a lot of reworking the way player parts are stored and identified. More notably, I’ve added the ability to sort and filter the items. This wasn’t really necessary at the moment since there’s 6 items, but it will be good later. This is the same attitude I have with adding most things at the moment. It’s a lot easier to add new features when there are things in the game. It makes everything a lot faster.

The shop can now be opened with the O key (I picked it because there’s an O in shop and s is taken. H is in the middle of the keyboard and I don’t like going there. I’ll probably change it later if it proves to be bad.) It can be closed with the escape key. There’s also a big red button in the corner which you can use to get out of the shop. The new feature that I’m quite proud of is the sorting and filtering. I like the way I did it and I’ll show the code below. As always, you can also look at the complete code on my GitHub. Here’s the store in its current state:

You can see many new UI elements. From left to right, top to bottom, you can see:

  • Show Owned and Show Unowned. When the show owned button is unchecked, it hides everything that the player currently owns. When the show unowned toggle is unchecked, it hides everything that the player doesn’t currently own.
  • The search bar lets you filter by name. The go button searches. The x button clears your current search filter.
  • Filter by affordable lets you filter by whether you can afford the items or not.
  • The name and price buttons let you switch your sorting mode. Clicking a button will switch the sorting mode to that. For example, clicking price will sort the items from most expensive to least expensive. Clicking the button that you’re already sorting by will switch from ascending to descending, or back again.
  • The final element is a drop down list. Clicking the items in this list will sort by the different types of items.

These all work on top of each other so you can filter by multiple things if you want. You can only sort with 1 sorting method at a time.

Now I’ll show you how I do the sorting and filtering in the code. I like this code. I think it’s pretty. I won’t get very deep in how it actually works, I’ll just point out my favourite parts.

public void FilterItems() {
	foreach (ShopItem i in shopItems) {

	if (!showOwned && !showUnowned) {

	List<ShopItem> il = new List<ShopItem>();
	shopItems.ForEach((x) => {
		bool owned = Player.instance.ownedItems.Contains(x);
		if ((owned && !showOwned) || (!owned && !showUnowned)) {
		if (filterByAffordable && x.price > Player.instance.coins) {
		if (!string.IsNullOrEmpty(searchTerm) && ! {
		if (typeFilter != "All" && typeFilter != x.type.ToString()) {

	if (currentSorting == nameSortingButton) {
		il.Sort((x, y) => { return x.itemName.CompareTo(y.itemName); });
	} else if (currentSorting == priceSortingButton) {
		il.Sort((x, y) => { return x.price.CompareTo(y.price); });

	if (!sortAscending) {

	for (int i = 0; i < il.Count; i++) {

I like lines 11-25 the most. This code goes through every filter in order. I put the quickest code first so that it will spend as little time as possible on filtering. It won’t have to waste time checking the more performance-intensive parameters.

On 29 and 31, I use C#’s amazing sort function. It’s a bit confusing and I lasted a while sorting manually because I didn’t know how to use delegates, but man, they make it so much more convenient. I love delegates and I love lambdas and I love the Sort function. Thanks C# devs.

On 34-36, I just reverse the list if it’s supposed to be sorting descended instead of ascended. This probably isn’t efficient but it works well.

That’s everything interesting about my code. You can view all my code on GitHub if you want to. 

If you were skipping past the code section, you can open your eyes again. 

That’s everything I have for you. The next thing on the list (you can find it on my Trello), is to give the player health and a health bar. I should have that done by next week. I also need to adjust the way blocks work so that they can have scripts that run on them. Next week should be useful but not very interesting for you, so sorry about that. Thanks for reading, see you next week!

Leave a Reply