tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: This is just plain ... odd.
Date Fri, 09 Nov 2012 05:08:07 GMT
Hash: SHA1


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.

- -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[] {

        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;
            Collections.sort(fruitList, sorter);
            System.out.println("Sort " + (++i) + ": " + fruitList);
        System.out.println("Stopped after " + i + " iterations because
the list did not sort the same way.");

    static class BrokenSorter
      implements Comparator<String>
        public int compare(String a, String b)
              return 1;
          return a.compareTo(b);
    static class WorkingSorter
      implements Comparator<String>
        public int compare(String a, String b)
              return 1;

              return -1;

          return a.compareTo(b);
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools -
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message