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(List<String> columnNames) {
026 this(columnNames.toArray(new String[columnNames.size()]));
027 }
028
029 public ColumnsComparator(String columnName) {
030 this(new String[] {columnName});
031 }
032
033 public ColumnsComparator(String[] columnNames) {
034 _columnNames = columnNames;
035 }
036
037 @Override
038 public int compare(Object obj1, Object obj2) {
039 Object[] column1 = (Object[])obj1;
040 Object[] column2 = (Object[])obj2;
041
042 String columnName1 = (String)column1[0];
043 String columnName2 = (String)column2[0];
044
045 int x = -1;
046
047 for (int i = 0; i < _columnNames.length; i++) {
048 if (_columnNames[i].equals(columnName1)) {
049 x = i;
050
051 break;
052 }
053 }
054
055 int y = -1;
056
057 for (int i = 0; i < _columnNames.length; i++) {
058 if (_columnNames[i].equals(columnName2)) {
059 y = i;
060
061 break;
062 }
063 }
064
065 if ((x == -1) && (y > -1)) {
066 return 1;
067 }
068 else if ((x > -1) && (y == -1)) {
069 return -1;
070 }
071 else if ((x > -1) && (y > -1)) {
072 if (x < y) {
073 return -1;
074 }
075 else if (x > y) {
076 return 1;
077 }
078 }
079
080 return 0;
081 }
082
083 private final String[] _columnNames;
084
085 }