1 /* 2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 8 * - Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 11 * - Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * - Neither the name of Oracle nor the names of its 16 * contributors may be used to endorse or promote products derived 17 * from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 20 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 26 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 27 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * This source code is provided to illustrate the usage of a given feature 34 * or technique and has been deliberately simplified. Additional steps 35 * required for a production-quality application, such as security checks, 36 * input validation and proper error handling, might not be present in 37 * this sample code. 38 */ 39 40 package comparator; 41 42 import java.util.Arrays; 43 import java.util.Random; 44 import static java.util.stream.Collectors.joining; 45 46 public class Test { 47 private final static Random random = new Random(); 48 49 /** 50 * Simple case: sorting 1 .. 11 51 */ 52 private static String[] source1() { 53 return new String[] { 54 "java 1", 55 "java 2", 56 "java 3", 57 "java 4", 58 "java 5", 59 "java 6", 60 "java 7", 61 "java 8", 62 "java 9", 63 "java 10", 64 "java 11", 65 }; 66 } 67 68 /** 69 * Leading zeros and multiple numeric parts 70 */ 71 private static String[] source2() { 72 return new String[] { 73 "string", 74 "string0", 75 "string00", 76 "string1", 77 "string01", 78 "string001", 79 "string2", 80 "string02", 81 "string002", 82 "string002.a", 83 "string002.a0", 84 "string002.a1", 85 "string0002", 86 "string0002", 87 }; 88 } 89 90 /** 91 * Sample from MSDN 92 */ 93 private static String[] source3() { 94 return new String[] { 95 "2string", 96 "3string", 97 "20string", 98 "st2ring", 99 "st3ring", 100 "st20ring", 101 "string2", 102 "string3", 103 "string20", 104 }; 105 } 106 107 /** 108 * Fullwidth characters 109 */ 110 private static String[] source4() { 111 return new String[] { 112 "string 0", 113 "string 00", 114 "string 000", 115 "string 1", 116 "string 01", 117 "string 2", 118 "string 3", 119 "string 4", 120 "string 5", 121 "string 6", 122 "string 7", 123 "string 8", 124 "string 9", 125 "string 10", 126 "string 11", 127 "string 12", 128 }; 129 } 130 131 public static void main(String[] args) throws Exception { 132 test(source1()); 133 test(source2()); 134 test(source3()); 135 test(source4()); 136 System.out.println("Okay"); 137 } 138 139 private static void test(String[] strings) throws Exception { 140 String[] shuffled = (String[])strings.clone(); 141 for (int i = 0; i < shuffled.length - 1; ++i) { 142 int j = i + random.nextInt(shuffled.length - i); 143 String tmp = shuffled[i]; 144 shuffled[i] = shuffled[j]; 145 shuffled[j] = tmp; 146 } 147 148 Arrays.sort(shuffled, new AlphaNumericComparator()); 149 150 for (int i = 0; i < strings.length - 1; ++i) { 151 if (!strings[i].equals(shuffled[i])) { 152 throw new Exception("Wrong sorting order!\n" + 153 Arrays.stream(shuffled).collect(joining("\n"))); 154 } 155 } 156 } 157 }