Guest User

Natural Order Numerical Sorting

a guest
Dec 27th, 2014
327
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.80 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. void setup() {
  4.  
  5.   ArrayList<String> list = new ArrayList<String>();  
  6.   list.add("img10.jpg");
  7.   list.add("img20.jpg");
  8.   list.add("img1.jpg");
  9.   list.add("img2.jpg");
  10.  
  11.   Collections.sort(list, new AlphaNumericSorter() );
  12.   println( list );  
  13. }
  14.  
  15. void draw() {
  16.  
  17. }
  18.  
  19. // Code from http://stackoverflow.com/questions/1262239/natural-sort-order-string-comparison-in-java-is-one-built-in
  20. /**
  21.  * Sorter that compares the given Alpha-numeric strings. This iterates through each characters to
  22.  * decide the sort order. There are 3 possible cases while iterating,
  23.  *
  24.  * <li>If both have same non-digit characters then the consecutive characters will be considered for
  25.  * comparison.</li>
  26.  *
  27.  * <li>If both have numbers at the same position (with/without non-digit characters) the consecutive
  28.  * digit characters will be considered to form the valid integer representation of the characters
  29.  * will be taken and compared.</li>
  30.  *
  31.  * <li>At any point if the comparison gives the order(either > or <) then the consecutive characters
  32.  * will not be considered.</li>
  33.  *
  34.  * For ex., this will be the ordered O/P of the given list of Strings.(The bold characters decides
  35.  * its order) <i><b>2</b>b,<b>100</b>b,a<b>1</b>,A<b>2</b>y,a<b>100</b>,</i>
  36.  *
  37.  * @author kannan_r
  38.  *
  39.  */
  40. class AlphaNumericSorter implements Comparator<String>
  41. {
  42.     /**
  43.      * Does the Alphanumeric sort of the given two string
  44.      */
  45.     public int compare(String theStr1, String theStr2)
  46.     {
  47.         char[] theCharArr1 = theStr1.toCharArray();
  48.         char[] theCharArr2 = theStr2.toCharArray();
  49.         int aPosition = 0;
  50.         if (Character.isDigit(theCharArr1[aPosition]) && Character.isDigit(theCharArr2[aPosition]))
  51.         {
  52.             return sortAsNumber(theCharArr1, theCharArr2, aPosition++ );
  53.         }
  54.         return sortAsString(theCharArr1, theCharArr2, 0);
  55.     }
  56.  
  57.     /**
  58.      * Sort the given Arrays as string starting from the given position. This will be a simple case
  59.      * insensitive sort of each characters. But at any given position if there are digits in both
  60.      * arrays then the method sortAsNumber will be invoked for the given position.
  61.      *
  62.      * @param theArray1 The first character array.
  63.      * @param theArray2 The second character array.
  64.      * @param thePosition The position starting from which the calculation will be done.
  65.      * @return positive number when the Array1 is greater than Array2<br/>
  66.      *         negative number when the Array2 is greater than Array1<br/>
  67.      *         zero when the Array1 is equal to Array2
  68.      */
  69.     private int sortAsString(char[] theArray1, char[] theArray2, int thePosition)
  70.     {
  71.         int aResult = 0;
  72.         if (thePosition < theArray1.length && thePosition < theArray2.length)
  73.         {
  74.             aResult = (int)theArray1[thePosition] - (int)theArray2[thePosition];
  75.             if (aResult == 0)
  76.             {
  77.                 ++thePosition;
  78.                 if (thePosition < theArray1.length && thePosition < theArray2.length)
  79.                 {
  80.                     if (Character.isDigit(theArray1[thePosition]) && Character.isDigit(theArray2[thePosition]))
  81.                     {
  82.                         aResult = sortAsNumber(theArray1, theArray2, thePosition);
  83.                     }
  84.                     else
  85.                     {
  86.                         aResult = sortAsString(theArray1, theArray2, thePosition);
  87.                     }
  88.                 }
  89.             }
  90.         }
  91.         else
  92.         {
  93.             aResult = theArray1.length - theArray2.length;
  94.         }
  95.         return aResult;
  96.     }
  97.  
  98.     /**
  99.      * Sorts the characters in the given array as number starting from the given position. When
  100.      * sorted as numbers the consecutive characters starting from the given position upto the first
  101.      * non-digit character will be considered.
  102.      *
  103.      * @param theArray1 The first character array.
  104.      * @param theArray2 The second character array.
  105.      * @param thePosition The position starting from which the calculation will be done.
  106.      * @return positive number when the Array1 is greater than Array2<br/>
  107.      *         negative number when the Array2 is greater than Array1<br/>
  108.      *         zero when the Array1 is equal to Array2
  109.      */
  110.     private int sortAsNumber(char[] theArray1, char[] theArray2, int thePosition)
  111.     {
  112.         int aResult = 0;
  113.         int aNumberInStr1;
  114.         int aNumberInStr2;
  115.         if (thePosition < theArray1.length && thePosition < theArray2.length)
  116.         {
  117.             if (Character.isDigit(theArray1[thePosition]) && Character.isDigit(theArray1[thePosition]))
  118.             {
  119.                 aNumberInStr1 = getNumberInStr(theArray1, thePosition);
  120.                 aNumberInStr2 = getNumberInStr(theArray2, thePosition);
  121.  
  122.                 aResult = aNumberInStr1 - aNumberInStr2;
  123.  
  124.                 if (aResult == 0)
  125.                 {
  126.                     thePosition = getNonDigitPosition(theArray1, thePosition);
  127.                     if (thePosition != -1)
  128.                     {
  129.                         aResult = sortAsString(theArray1, theArray2, thePosition);
  130.                     }
  131.                 }
  132.             }
  133.             else
  134.             {
  135.                 aResult = sortAsString(theArray1, theArray2, ++thePosition);
  136.             }
  137.         }
  138.         else
  139.         {
  140.             aResult = theArray1.length - theArray2.length;
  141.         }
  142.         return aResult;
  143.     }
  144.  
  145.     /**
  146.      * Gets the position of the non digit character in the given array starting from the given
  147.      * position.
  148.      *
  149.      * @param theCharArr /the character array.
  150.      * @param thePosition The position after which the array need to be checked for non-digit
  151.      *        character.
  152.      * @return The position of the first non-digit character in the array.
  153.      */
  154.     private int getNonDigitPosition(char[] theCharArr, int thePosition)
  155.     {
  156.         for (int i = thePosition; i < theCharArr.length; i++ )
  157.         {
  158.             if ( !Character.isDigit(theCharArr[i]))
  159.             {
  160.                 return i;
  161.             }
  162.         }
  163.         return -1;
  164.     }
  165.  
  166.     /**
  167.      * Gets the integer value of the number starting from the given position of the given array.
  168.      *
  169.      * @param theCharArray The character array.
  170.      * @param thePosition The position form which the number need to be calculated.
  171.      * @return The integer value of the number.
  172.      */
  173.     private int getNumberInStr(char[] theCharArray, int thePosition)
  174.     {
  175.         int aNumber = 0;
  176.         for (int i = thePosition; i < theCharArray.length; i++ )
  177.         {
  178.             if(!Character.isDigit(theCharArray[i]))
  179.             {
  180.                return aNumber;
  181.             }
  182.             aNumber += aNumber * 10 + (theCharArray[i] - 48);
  183.         }
  184.         return aNumber;
  185.     }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment