/*
 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 2.1 which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/lgpl.html
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * Contributors:
 *     Nuxeo - initial API and implementation
 *
 * $Id$
 */

package org.nuxeo.ecm.platform.ui.web.directory;

import java.io.Serializable;
import java.util.Comparator;

import javax.faces.model.SelectItem;

/**
 * @author <a href="mailto:glefter@nuxeo.com">George Lefter</a>
 */
public class SelectItemComparator implements Comparator<SelectItem>, Serializable {

    private static final long serialVersionUID = -2823867424119790285L;

    private final String ordering;

    public SelectItemComparator(String ordering) {
        this.ordering = ordering;
    }

    public int compare(SelectItem item1, SelectItem item2) {
        if (ordering.equals("label")) {
            return item1.getLabel().compareTo(item2.getLabel());
        } else if (ordering.equals("id")) {
            return ((String) item1.getValue()).compareTo((String) item2.getValue());
        } else {
            throw new RuntimeException("invalid sort criteria");
        }
    }

}

@@ -965,16 +965,24 @@
 ctItem%3E,
+%0A       
  Seriali
@@ -1191,16 +1191,414 @@
 %0A    %7D%0A%0A
+    protected int compare(String item1, String item2) %7B%0A        // deal with numbers comparison%0A        try %7B%0A            Integer int1 = Integer.valueOf(item1);%0A            Integer int2 = Integer.valueOf(item2);%0A            return int1.compareTo(int2);%0A        %7D catch (NumberFormatException e) %7B%0A            // let default comparison happen%0A        %7D%0A        return item1.compareTo(item2);%0A    %7D%0A%0A
     publ
@@ -1705,24 +1705,32 @@
      return 
+compare(
 item1.getLab
@@ -1733,27 +1733,18 @@
 tLabel()
-.compareTo(
+, 
 item2.ge
@@ -1821,55 +1821,85 @@
 urn 
-((String) item1.getValue()).compareTo((String) 
+compare(String.valueOf(item1.getValue()),%0A                    String.valueOf(
 item
@@ -1911,16 +1911,17 @@
 Value())
+)
 ;%0A      
@@ -2012,11 +2012,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

Fix select items ordering of integers