001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools.comparator;
016    
017    import java.util.Comparator;
018    import java.util.List;
019    
020    /**
021     * @author Brian Wing Shun Chan
022     */
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    }