001
014
015 package com.liferay.portal.tools.comparator;
016
017 import java.util.Comparator;
018 import java.util.List;
019
020
023 public class ColumnsComparator implements Comparator<Object> {
024
025 public ColumnsComparator(String columnName) {
026 this(new String[] {columnName});
027 }
028
029 public ColumnsComparator(List<String> columnNames) {
030 this(columnNames.toArray(new String[columnNames.size()]));
031 }
032
033 public ColumnsComparator(String[] columnNames) {
034 _columnNames = columnNames;
035 }
036
037 public int compare(Object obj1, Object obj2) {
038 Object[] column1 = (Object[])obj1;
039 Object[] column2 = (Object[])obj2;
040
041 String columnName1 = (String)column1[0];
042 String columnName2 = (String)column2[0];
043
044 int x = -1;
045
046 for (int i = 0; i < _columnNames.length; i++) {
047 if (_columnNames[i].equals(columnName1)) {
048 x = i;
049
050 break;
051 }
052 }
053
054 int y = -1;
055
056 for (int i = 0; i < _columnNames.length; i++) {
057 if (_columnNames[i].equals(columnName2)) {
058 y = i;
059
060 break;
061 }
062 }
063
064 if ((x == -1) && (y > -1)) {
065 return 1;
066 }
067 else if ((x > -1) && (y == -1)) {
068 return -1;
069 }
070 else if ((x > -1) && (y > -1)) {
071 if (x < y) {
072 return -1;
073 }
074 else if (x > y) {
075 return 1;
076 }
077 }
078
079 return 0;
080 }
081
082 private String[] _columnNames;
083
084 }