/* A wrapper for some common sorting/filtering methods etc, mostly using java.util.Collections */ public static class TransactionUtils { //sort a List of transactions by the title public static void sortTransactionsByTitle(ArrayList transactions) { Collections.sort(transactions, new Comparator() { public int compare(Object o1, Object o2) { Transaction t1 = (Transaction) o1; Transaction t2 = (Transaction) o2; return t1.title.compareToIgnoreCase(t2.title); } } ); } public static void sortWords(HashMap words) { Iterator it; Map.Entry me; Word w; sortedWords.clear(); it = words.entrySet().iterator(); // Get an iterator //skim through the word set and add it to an array list while(it.hasNext()){ me = (Map.Entry)it.next(); w = (Word) me.getValue(); if(w.totFreq > MINVALUE){ sortedWords.add(w); } } Collections.sort(sortedWords, new Comparator() { public int compare(Object o1, Object o2) { Word t1 = (Word) o1; Word t2 = (Word) o2; return t1.word.compareToIgnoreCase(t2.word); } } ); } public static void sortValues(HashMap words) { Iterator it; Map.Entry me; Word w; sortedWords.clear(); it = words.entrySet().iterator(); // Get an iterator //skim through the word set and add it to an array list while(it.hasNext()){ me = (Map.Entry)it.next(); w = (Word) me.getValue(); if(w.totFreq > MINVALUE){ sortedWords.add(w); } } Collections.sort(sortedWords, new Comparator() { public int compare(Object o1, Object o2) { Word t1 = (Word) o1; Word t2 = (Word) o2; return t2.totFreq - t1.totFreq; } } ); } //reverse order of List public static void reverse(ArrayList transactions) { Collections.reverse(transactions); } public static void filterOutNonAlphabeticalTitles(ArrayList transactions) { for (int i = transactions.size() - 1; i >= 0; i--) { Transaction t = (Transaction) transactions.get(i); if (t.title.length() < 1) { transactions.remove(t); continue; } char firstLetter = t.title.charAt(0); if (! ((firstLetter >= 'a' && firstLetter <= 'z') || (firstLetter >= 'A' && firstLetter <= 'Z')) ) { transactions.remove(t); continue; } } } }