2 Replies Latest reply on Sep 20, 2007 12:52 AM by zorglub76

    Bug in numeric sorting?

    zorglub76
      I can't get rid of this problem...
      I have a list of items (custom rendered). I bound some data to it from HTTPService object. One of the fields in data is "Key" and it looks like this "ITEM-1", "ITEM-2"......"ITEM-10".
      When I do this:

      var sort:Sort = new Sort();
      var sf:SortField = new SortField("Key");
      sf.numeric = true;
      sort.fields = [sf];

      and use this object for sorting my data, the items in the list get sorted like this:

      6 - 2 - 3 - 4 - 5 - 1 - 7 - 8 - 9 - 10

      And no matter how much items I have, I always get the following list:
      (n/2+1) - 2 - 3 - 4 - ... - (n/2-2) - (n/2-1) - (n/2) - 1 - (n/2+2) - (n/2+3) - (n/2+4) - .....

      I tried with the custom compareFunction on SortField object, extracted just the number from the value (i.e. extracted 18 from "ITEM-18" and cast it to int) and compared just the numbers, but the list stayed the same.

      I use flex data services 2.0.1

      Any ideas??

        • 1. Re: Bug in numeric sorting?
          Senor_Roberto Level 1
          Try sorting it alphabetically (i.e. default).

          I've written some test code below and AS3 clearly has a bit of a problem when trying to convert "ITEM-6" etc to a number. It doesn't just seem to be parsing out the first integer, it seems to be trying to convert it some other way...

          This test code:

          var testData:Array = [{Key: "ITEM-1"}, {Key: "ITEM-6"}, {Key: "ITEM-3"}, {Key: "ITEM-99"}];

          var testCollection:ArrayCollection = new ArrayCollection(testData);

          var sorter:Sort = new Sort();
          var field:SortField = new SortField("Key");

          sorter.fields = [field];

          trace("Sorting alphabetically...");
          testCollection.sort = sorter;
          testCollection.refresh();

          var index:Number = 0;
          for each (var i:* in testCollection) {
          trace("Position " + index + ": " + i.Key);
          }

          trace("Sorting numerically...");
          testCollection.sort.fields[0].numeric = true;
          testCollection.refresh();

          index = 0;
          for each (var j:* in testCollection) {
          trace("Position " + index + ": " + j.Key);
          }

          Produces the following trace output:

          Sorting alphabetically...
          Position 0: ITEM-1
          Position 0: ITEM-3
          Position 0: ITEM-6
          Position 0: ITEM-99
          Sorting numerically...
          Position 0: ITEM-3
          Position 0: ITEM-6
          Position 0: ITEM-1
          Position 0: ITEM-99

          So the alphabetical version sorts correctly as expected, but the numeric sort produces the strange results you're posting about. It would be interesting to hear from Adobe on this, but in the meantime I would suggest using the alpha sort :)

          As an aside, it would be really nice to have similar functionality to Javas Comparable/Comparator interfaces in collection sorting. Has anyone written anything like this? Perhaps I should...
          • 2. Bug in numeric sorting?
            zorglub76 Level 1
            Hmm... I tried numeric sorting because I originally had problems with alphabetical sorting (I was getting the following order: ITEM-1, ITEM-10, ITEM-100, ITEM-2, ...., ITEM-99, or something like that). I believe your example would have the same result if you entered ITEM-10 into the array....
            The problem probably lies in sorting algorithm. I really don't know much about sorting algorithms, but as far as I know, for quick sort algorithm, you take the median member of the array and compare it with first, or something like that... Since here first and median items are switched, I believe the problem lies in implementation of the algorithm... I am only surprised no one noticed it yet...