Java ArrayList Sort sortSignatures(ArrayList sList)

Here you can find the source of sortSignatures(ArrayList sList)

Description

Sort first by method name, then by parm list number and value

License

Open Source License

Parameter

Parameter Description
sList collection of method signatures

Declaration

static public void sortSignatures(ArrayList<String> sList) 

Method Source Code

//package com.java2s;

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

public class Main {
    static private final String COMMA = ",";
    static private final String SPACE = " ";
    static private final String LEFT_PAREN = "(";
    static private final String RIGHT_PAREN = ")";

    /**//from  www.  j a v a  2  s  . c  o  m
     * Sort first by method name, then by parm list number and value
     * 
     * @param sList collection of method signatures
     */
    static public void sortSignatures(ArrayList<String> sList) {
        Collections.sort(sList, new Comparator<String>() {
            @Override
            public int compare(String sig1, String sig2) {
                // Tokenize into three parts: method name, parm list, return type
                String name1 = sig1.substring(sig1.indexOf(SPACE) + 1,
                        sig1.indexOf(LEFT_PAREN));
                String name2 = sig2.substring(sig2.indexOf(SPACE) + 1,
                        sig2.indexOf(LEFT_PAREN));
                String parms1 = sig1.substring(sig1.indexOf(LEFT_PAREN),
                        sig1.indexOf(RIGHT_PAREN) + 1);
                String parms2 = sig2.substring(sig2.indexOf(LEFT_PAREN),
                        sig2.indexOf(RIGHT_PAREN) + 1);
                // System.err.println("\t\t sort loops = " + ++count);

                // Compare method names
                int retval = name1.compareTo(name2); // compare method names
                // Compare number of parms and parms names
                if (retval == 0) {
                    String[] nbrParms1 = parms1.split(COMMA);
                    String[] nbrParms2 = parms2.split(COMMA);
                    retval = nbrParms1.length - nbrParms2.length;
                    if (retval == 0) {
                        retval = parms1.compareTo(parms2);
                    }
                }
                return retval;
            }
        });
    }
}

Related

  1. sortArray(ArrayList list)
  2. sortByNibble(ArrayList symbolAL, int delimiter)
  3. sortedPatterns(ArrayList patterns)
  4. sortImports(ArrayList imports)
  5. SortStringList(ArrayList list)