# Filter option for PFQueryTableViewController in Swift

I have a PFQueryTableViewController, and have create a separate TableViewController that allows me to select a filter option. When the user selects an option to filter by, I pass the index value back, and then append that Int value to an array. The variable is declared at the top like:

var indexArray = [0]


In my QueryForTable() function, I've written:

    if indexArray == [0] {

query.orderByDescending("createdAt")

} else if indexArray == [1] {

query.orderByAscending("FilmName")

} else if indexArray == [2] {

query.orderByDescending("OurRating")

} else if indexArray == [3] {

query.orderByAscending("DirectedBy")

} else if indexArray == [4] {

query.orderByDescending("UpVoteCount")

} else if indexArray == [5] {

query.orderByDescending("DownVoteCount")

}


This code works fine, and is filtering the option fine.

I'm wondering if there is a better way to write the above code in my QueryForTable() function? Maybe to reduce the amount of code needed?

• Well, if your plan is to filter by multiple properties at once, I can't see how this works. Also, there doesn't seem to be a way to, for example, sort by "createdAt" in ascending order. – nhgrif Apr 1 '16 at 12:04
• Why not use a switch statement instead of multiple else ifs? – kye Apr 2 '16 at 0:13