src/java/org/pifan/util/StringUtils.java
/**
     * The Alphanum Algorithm is an improved sorting algorithm for strings
     * containing numbers.  Instead of sorting numbers in ASCII order like
     * a standard sort, this algorithm sorts numbers in numeric order.
     *
     * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com    
     *      
     * @param deviceId1
     * @param deviceId2 
     * @return
     * 
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     */
The Alphanum Algorithm is an improved sorting algorithm for strings
containing numbers.  Instead of sorting numbers in ASCII order like
a standard sort, this algorithm sorts numbers in numeric order.



@param deviceId1
@param deviceId2
@return


The Alphanum Algorithm is an improved sorting algorithm for strings containing numbers . Instead of sorting numbers in ASCII order like a standard sort this algorithm sorts numbers in numeric order . @param deviceId1 @param deviceId2 @return
public static int sortAlphanumericValues(String deviceId1, String deviceId2) {
        try {
            int thisMarker = 0;
            int thatMarker = 0;
            int s1Length = deviceId1.length();
            int s2Length = deviceId2.length();
            while (thisMarker < s1Length && thatMarker < s2Length) {
                String thisChunk = getChunk(deviceId1, s1Length, thisMarker);
                thisMarker += thisChunk.length();
                String thatChunk = getChunk(deviceId2, s2Length, thatMarker);
                thatMarker += thatChunk.length();
                int result = 0;
                if (StringUtils.isDigit(thisChunk.charAt(0)) && StringUtils.isDigit(thatChunk.charAt(0))) {
                    int thisChunkLength = thisChunk.length();
                    result = thisChunkLength - thatChunk.length();
                    if (result == 0) {
                        for (int i = 0; i < thisChunkLength; i++) {
                            result = thisChunk.charAt(i) - thatChunk.charAt(i);
                            if (result != 0) {
                                return result;
                            }
                        }
                    }
                } else {
                    result = thisChunk.compareTo(thatChunk);
                }
                if (result != 0) {
                    return result;
                }
            }
            return s1Length - s2Length;
        } catch (Exception ex) {
            return deviceId1.compareToIgnoreCase(deviceId2);
        }
    }
public static int sortAlphanumericValues ( String deviceId1 , String deviceId2 ) { try { int thisMarker = 0 ; int thatMarker = 0 ; int s1Length = deviceId1 . length ( ) ; int s2Length = deviceId2 . length ( ) ; while ( thisMarker < s1Length && thatMarker < s2Length ) { String thisChunk = getChunk ( deviceId1 , s1Length , thisMarker ) ; thisMarker += thisChunk . length ( ) ; String thatChunk = getChunk ( deviceId2 , s2Length , thatMarker ) ; thatMarker += thatChunk . length ( ) ; int result = 0 ; if ( StringUtils . isDigit ( thisChunk . charAt ( 0 ) ) && StringUtils . isDigit ( thatChunk . charAt ( 0 ) ) ) { int thisChunkLength = thisChunk . length ( ) ; result = thisChunkLength - thatChunk . length ( ) ; if ( result == 0 ) { for ( int i = 0 ; i < thisChunkLength ; i ++ ) { result = thisChunk . charAt ( i ) - thatChunk . charAt ( i ) ; if ( result != 0 ) { return result ; } } } } else { result = thisChunk . compareTo ( thatChunk ) ; } if ( result != 0 ) { return result ; } } return s1Length - s2Length ; } catch ( Exception ex ) { return deviceId1 . compareToIgnoreCase ( deviceId2 ) ; } }
The Alphanum Algorithm is an improved sorting algorithm for strings
 containing numbers.
The Alphanum Algorithm is an improved sorting algorithm for strings containing numbers .
// If both chunks contain numeric characters, sort them numerically
 // Simple chunk comparison by length.
 // If equal, the first different number counts
 // just compare by strings
