6
\$\begingroup\$

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?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Enum

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<T>.

If you want to use an enum and reorder inside the application, then I would let the enum take a Comparator<T> in the constructor and create an anonymous inner class for each enum value.

\$\endgroup\$
4
  • \$\begingroup\$ I never new enums were so flexible. \$\endgroup\$
    – sotrh
    Jul 11, 2014 at 15:21
  • \$\begingroup\$ Is the 'asc' necessary? Isn't that implied? \$\endgroup\$
    – sotrh
    Jul 11, 2014 at 15:23
  • 4
    \$\begingroup\$ @sotrh Enums in Java is among the best there is. The asc is not necessary, but I feel that it's better to have it there for clarity. \$\endgroup\$ Jul 11, 2014 at 15:27
  • \$\begingroup\$ @sotrh No problem, remember that now that you have more than 15 reputation, you also have the power to vote! Voting is very important on this site as we're trying to graduate some day. Welcome to Code Review! \$\endgroup\$ Jul 11, 2014 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.