/* * [Shuffle.java] * * Summary: Shuffle items into a different random order each time it is run. * * Copyright: (c) 2014-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 1.0 2014-05-01 initial version */ package com.mindprod.sortcode; import com.mindprod.common18.FNV1a64; import java.util.Comparator; import java.util.Random; /** * Shuffle items into a different random order each time it is run. *

* compare two blocks of text to sort them into a different random order each time this is run. * * @author Roedy Green, Canadian Mind Products * @version 1.0 2014-05-01 initial version * @since 2014-05-01 */ public class Shuffle implements Comparator { /** * randomiser that stays constant through one sort */ private static final long SEED = new Random().nextLong(); private static long computeKey( String s ) { // this ensures Shuffle is consistent within each run of the desired final order. // we don't want to drive Collections.sort mad by changing keys in the middle of a sort. return FNV1a64.computeHash( s ) ^ SEED; } // /method /** * Compare two Strings * * @param a first String to compare * @param b second String to compare * * @return +ve if a>b, 0 if a==b, -ve if a<b */ public final int compare( String a, String b ) { return Long.compare( computeKey( a ), computeKey( b ) ); } // /methods } // end class