Java List Sort sortRepositorynames(List list)

Here you can find the source of sortRepositorynames(List list)

Description

Sort grouped repository names.

License

Apache License

Parameter

Parameter Description
list a parameter

Declaration

public static void sortRepositorynames(List<String> list) 

Method Source Code

//package com.java2s;
/*//from   w ww  . j  av  a2s. c  o  m
 * Copyright 2011 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.util.Collections;
import java.util.Comparator;

import java.util.List;

public class Main {
    /**
     * Sort grouped repository names.
     *
     * @param list
     */
    public static void sortRepositorynames(List<String> list) {
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return compareRepositoryNames(o1, o2);
            }
        });
    }

    /**
     * Compare two repository names for proper group sorting.
     *
     * @param r1
     * @param r2
     * @return
     */
    public static int compareRepositoryNames(String r1, String r2) {
        // sort root repositories first, alphabetically
        // then sort grouped repositories, alphabetically
        r1 = r1.toLowerCase();
        r2 = r2.toLowerCase();
        int s1 = r1.indexOf('/');
        int s2 = r2.indexOf('/');
        if (s1 == -1 && s2 == -1) {
            // neither grouped
            return r1.compareTo(r2);
        } else if (s1 > -1 && s2 > -1) {
            // both grouped
            return r1.compareTo(r2);
        } else if (s1 == -1) {
            return -1;
        } else if (s2 == -1) {
            return 1;
        }
        return 0;
    }
}

Related

  1. sortLoops(ArrayList list)
  2. sortMap(Map targetMap, List sortedList)
  3. sortMapEntryListByValue(List> entries, boolean descending)
  4. sortMessages(List ms)
  5. sortNames(List nameLs, String splitStr)
  6. sortSimpleName(List list)
  7. sortSimpleName(List list)
  8. sortString(Set lists)
  9. sortStringEnumeration(Enumeration list)