-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Russ,
On 11/8/12 6:05 PM, Russ Kepler wrote: > On Thursday, November 08, 2012 07:36:20 PM Lyallex wrote: > >> The only difference between the two executions is the fact that >> the test code executes in it's own instance of the JVM whereas >> the other execution runs in an instance shared with the >> container. >> >> I accept that the behaviour may be undefined even though it is >> consistently repeatable in both environments but surely given >> everything else being equal the results should be the same ... or >> maybe I'm just losing the plot. > > No, you're right but just missing some small difference in the > environments. > > I'd verify that you get the same input data in the same order in > both cases, and that you're starting with the same size container > [...] After writing a bench test that I couldn't get to fail, your comment here tripped a thought in my brain: the "container" size. So, I added an element to my list of Strings and boom: failure. It turns out that the collection size doesn't matter: I just hadn't been iterating enough, so I added a loop that will run until the initial sorted order doesn't match the re-sorted order (with shuffles in between). Lyallex, see the code below: it will fail after a few iterations to produce the same element ordering. Switch from BrokenSorter to WorkingSorter and you'll find that it runs forever. Are you *sure* that your database always returns the items in the same order? If you plan on sorting alphabetically later, why bother sorting by id when fetching? Unless you are really sorting by id when fetching, the data can come back in any order. It may *often* be in entry-sequenced order, but it is certainly not guaranteed to be. The code below shows that, without any funny business, the sort can work sometimes and not in others. Enjoy, - -chris import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; public class SortTest { public static void main(String[] args) { String[] fruits = new String[] { "Apples", "Bananas", "Coconuts", "Dates", "Eggplants", "Figs", "Grapefruits", "Honeydews", "Ilamas", "Jambolans", "Kepels", "Lemons", "Miscellaneous", "Nectarines" }; List<String> fruitList = Arrays.asList(fruits); Comparator<String> sorter = new BrokenSorter(); System.out.println("Initial order: " + fruitList); Collections.sort(fruitList, sorter); System.out.println("Sort 1: " + fruitList); List<String> saved = new ArrayList<String>(fruitList); int i = 1; do { Collections.shuffle(fruitList); Collections.sort(fruitList, sorter); System.out.println("Sort " + (++i) + ": " + fruitList); } while(fruitList.equals(saved)); System.out.println("Stopped after " + i + " iterations because the list did not sort the same way."); } static class BrokenSorter implements Comparator<String> { @Override public int compare(String a, String b) { if(a.equals("Miscellaneous")) return 1; return a.compareTo(b); } } static class WorkingSorter implements Comparator<String> { @Override public int compare(String a, String b) { if(a.equals("Miscellaneous")) return 1; if(b.equals("Miscellaneous")) return -1; return a.compareTo(b); } } } -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.17 (Darwin) Comment: GPGTools - http://gpgtools.org Comment: Using GnuPG with Mozilla - http://www.enigmail.net/ iEYEARECAAYFAlCcj7cACgkQ9CaO5/Lv0PBpawCeORBT62XWcjyw+SruT6Bhkh50 sDEAn1ZjSiPR70+DV/QVBFOjXKjH498o =F3QS -----END PGP SIGNATURE----- --------------------------------------------------------------------- To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org