Filter and Sort

If I had a database of people and I had an application that displays this list, it makes sense that there would be a feature to search for particular people based on certain criteria. This is a rough and simplistic but good enough comparison to what I had to do at work recently.

Background

Because the program had already been created and I just needed to add this “small” feature, I didn’t really have a choice in the programming language. Not that it matters because 1.) I don’t want to be dealing with UI in c++ and 2.) even if I could use Java, it’s never a bad idea to learn another language. And so I wrote code in c# for the first time.

The program follows the Model-View-View Model (MVVM) architectural pattern using Windows Presentation Foundation (WPF). I learned what both things are for this particular job and studied them while looking at the existing code for reference (or otherwise just getting confused because the internet says conflicting things and the code my co-workers write of course aren’t perfect). Still not very confident about my understanding of it but it makes sense in my head (for now).

In order to add the requested feature into the program, I first had to decide how I wanted to make it work. I could, for example, add a row of drop-down menus (one for each information). Or, maybe I could change the drop-down to a textbox for some because if a person had a “Description of physical appearance” information, I can’t really make drop-down menu items out of paragraphs of text.

badui
Bad Horrible UI idea

There were so many ways to go about it, I would have been very confused if not for some very convenient truths:

  • Because the whole UI (or 99% of it at least) in WPF MVVM is writen in XML, I really didn’t want to deal with that confusion so I wanted to minimize the UI part I had to create.
  • This program is targeted towards programmers and not end-users so it doesn’t have to be intuitive for the general public. It just has to be something easy and understandable for most programmers (at least the ones who will use the program).
  • I wanted to learn more about and code more in c# than just a long list of conditions and List.sort().
  • LINQ seemed interesting.
  • Most importantly, my boss approved the final idea.

And that idea was: to add a single long textbox where a query can be entered.

filtersort

Solution

To start with, I had to decided on the following keywords: AND, OR, NOT, EQUALS, MATCHES, FROM, TO, ORDERBY, ASCENDING (or ASC), DESCENDING (or DESC), parentheses (  ), quotation marks ” “, and of course the information header (in the case of the Person above: Name, Age, Birthday, NumberOfPets, as I see fit).

 

query.png

The very first thing that must be done after getting the query is to parse it. Anything that has to do with string manipulation was something I’ve always enjoyed and it was one of the things I specialized in when I used to join (low-level) competitive programming.

And once the query is parsed, apply the filters and the sorting to the list of objects (in this case people) through the use of stacks. Data structures was another sub-category of competitive programming I loved. It helps that a lot of string-related problems are often intertwined with the use of data structures, as this particular work of mine shows.

Because I had to put both query keywords and lists of people into the stack, I created a special class that contains either a string or a list of people, and a boolean so I know which one it is.

I don’t even know if there’s already a library or tool somewhere in the vast web available for use to do this very feature. But I didn’t even try looking because I really wanted to do this myself. And I’m glad I did because it was really fun.

To make things more interesting, once I got the feature up and working, I set out to generalize the filter/sorting tool I created so it can be used for any class through the use of reflection. It took an additional 2 hours, including searching about reflection in c#, to get the generalized tool working.

Overall, really happy with the result and am contemplating putting it up privately on my GitHub in case I need it for personal use (why I’d ever need it I have no idea, but you never know…).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: