I'm writing an application for work that requires me to process a list of transactions. The list is small (5 records at the most) and I'm required to display that data in an android ListView
. My client has asked that I include the ability to order the list based on the date (I have added ordering it by the amount spent.)
My code uses SQLite queries to get the transactions, from an in-memory database, in the desired order.
public Cursor getAllTransactions(int orderByType)
{
String orderBy = " order by ";
switch (orderByType)
{
case ORDER_BY_NEWER:
orderBy += "date(" + KEY_DATE + ") desc";
break;
case ORDER_BY_OLDER:
orderBy += "date(" + KEY_DATE + ")";
break;
case ORDER_BY_MOST:
orderBy += KEY_AMOUNT;
break;
case ORDER_BY_LEAST:
orderBy += KEY_AMOUNT + " desc";
break;
default:
orderBy = null;
}
return getAllTransactions(orderBy);
}
The getAllTransactions(orderBy)
line executes the SQL command select * from TABLE_TRANSACTIONS
appending the order by
constraint should it be non-null.
Is this the proper use of SQL? Would it be more efficient (aka faster) to store this data in a standard container (such as List<Transaction>
) and then sort them in the desired order?