package com.lucasdnd.ags.ui; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Comparator; public class ListRowComparator implements Comparator<ListRow> { private int columnSort; public ListRowComparator(int columnSort) { this.columnSort = columnSort; } /** * Starts with this method, then sends it to the one with columnSort */ @Override public int compare(ListRow listRow1, ListRow listRow2) { // Get the the Column Text String text1 = listRow1.getListColumns()[columnSort].getText(); String text2 = listRow2.getListColumns()[columnSort].getText(); // Check if that column is a Text, Number or Date if(listRow1.getListColumns()[columnSort].getDataType() == ListColumn.TEXT) { // TEXT! return text1.compareToIgnoreCase(text2); } else if(listRow1.getListColumns()[columnSort].getDataType() == ListColumn.NUMBER) { // NUMBER! // Remove the "?" of locked games, the . and the , String cleanText1 = text1.replace(".", "").replace(",", "").replace("?", ""); String cleanText2 = text2.replace(".", "").replace(",", "").replace("?", ""); // Let's see if the String has a "/" String[] parts1 = cleanText1.split("/"); if(parts1.length > 1) { cleanText1 = parts1[1]; } String[] parts2 = cleanText2.split("/"); if(parts2.length > 1) { cleanText2 = parts2[1]; } // Parse to number int num1 = 0; int num2 = 0; // If there's something in the text, parse it to int if(cleanText1.length() > 0) { num1 = Integer.parseInt(cleanText1); } if(cleanText2.length() > 0) { num2 = Integer.parseInt(cleanText2); } // Compare! if(num1 > num2) { return 1; } else if(num2 > num1) { return -1; } else { return 0; } } else { // DATE! // Set the format SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy"); // Create the Dates long date1 = 0l; long date2 = 0l; // Convert the Dates try { date1 = format.parse(text1).getTime(); date2 = format.parse(text2).getTime(); } catch (ParseException e) { e.printStackTrace(); } if(date1 > date2) { return 1; } else if(date2 > date1) { return -1; } else { return 0; } } } }