patternjavaMinor
Using SQL to reorder items in a list view - is this efficient?
Viewed 0 times
thisreordersqlefficientviewitemsusinglist
Problem
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
My code uses SQLite queries to get the transactions, from an in-memory database, in the desired order.
The
Is this the proper use of SQL? Would it be more efficient (aka faster) to store this data in a standard container (such as
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) and then sort them in the desired order?Solution
Enum
First of all, instead of an
This will clean up your
Database vs. Application
I feel that is OK to let the database deal with this. There is a positive aspect of letting it be handled in the application: If the application deals with it, then you can reorder the elements without querying the database.
However,
The list is small (5 records at the most)
I think either way will work just fine. If it is common to reorder the list, then I would let the application deal with it. Although your
If you want to use an enum and reorder inside the application, then I would let the enum take a
First of all, instead of an
int orderByType, I would use an enum.enum SortOrder {
NEWER("date(" + SomeConstant.KEY_DATE + ") desc"),
OLDER("date(" + SomeConstant.KEY_DATE + ") asc"),
...;
private SortOrder(String order) {
this.order = order;
}
public String getOrder() {
return this.order;
}
}This will clean up your
getAllTransactions method:public Cursor getAllTransactions(SortOrder orderByType) {
return getAllTransactions(orderByType == null ? null : orderByType.getOrder());
}Database vs. Application
I feel that is OK to let the database deal with this. There is a positive aspect of letting it be handled in the application: If the application deals with it, then you can reorder the elements without querying the database.
However,
The list is small (5 records at the most)
I think either way will work just fine. If it is common to reorder the list, then I would let the application deal with it. Although your
enum SortOrder needs quite a lot of more code to work directly with a List.If you want to use an enum and reorder inside the application, then I would let the enum take a
Comparator in the constructor and create an anonymous inner class for each enum value.Code Snippets
enum SortOrder {
NEWER("date(" + SomeConstant.KEY_DATE + ") desc"),
OLDER("date(" + SomeConstant.KEY_DATE + ") asc"),
...;
private SortOrder(String order) {
this.order = order;
}
public String getOrder() {
return this.order;
}
}public Cursor getAllTransactions(SortOrder orderByType) {
return getAllTransactions(orderByType == null ? null : orderByType.getOrder());
}Context
StackExchange Code Review Q#56764, answer score: 4
Revisions (0)
No revisions yet.