You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@flex.apache.org by Shervin Asgari <sh...@webstep.no> on 2013/02/27 09:31:54 UTC

Specific sorting, then alphabetic

Hi.

I am very very new to Flex.
I created this thread for sorting in Java
http://stackoverflow.com/q/15091732/37298

However, I am now trying to do the same thing in ActionScript.

I have a Bindable ArrayCollection with Objects that looks like this

Code.code : F
Code.label: Foo
Code.description: The Foo description

Then I want to sort exactly as specified in the stackoverflow site.
I have around 10 of them which I need in a particular order. I have them
defined in an array.

Based on the stackoverflow question and some googling, I have come up with
the following:

private function myCompare(a:Object, b:Object, fields:Array = null):int {
            if (mostUsed.contains(a.code) && mostUsed.contains(b.code)) {
                return fields.getItemIndex(a) - fields.getItemIndex(b);
            }

            if (mostUsed.contains(a.code)) {
                return -1;
            }

            if (mostUsed.contains(b.code)) {
                return 1;
            }

            var s:Sort = new Sort();
            s.fields = fields;
            var f:Function = s.compareFunction;
            return f.call(null,a,b,fields);
        }

private var mostUsed:ArrayCollection = new ArrayCollection(["F", "A", "B",
"FTR", "HIS", "KEB", "LEG", "MED", "MER", "PEN", "PEO", "PER", "PIN",
"PUR", "REF"]);

var grunner:ArrayCollection = //getting collection
grunner.sort.fields = [new SortField("code")];
grunner.sort.compareFunction = myCompare;
grunner.refresh();

But it doesnt work. Any clues?
My collection returns empty.

Regards Shervin

Re: Specific sorting, then alphabetic

Posted by John Cunliffe <ma...@gmail.com>.
Not sure why you are implementing your own compare. Instead I would add an
integer sort property to your code object, where the specified order is
reflected. Of your n specially sorted objects, each gets 1...n value, all
the remaining being sorted elsewhere get n+1. Then you use multiple
sortfields with the first sorting on the sort property, and the second on
your code property.

See
http://livedocs.adobe.com/flex/3/html/help.html?content=advdatagrid_03.htmland
http://www.brucephillips.name/blog/index.cfm/2006/11/23/Sort-An-ArrayCollection-By-Multiple-Fields-and-Filter-An-ArrayCollection-By-Multiple-Fields-In-Flex

happy coding
John


On Wed, Feb 27, 2013 at 9:31 AM, Shervin Asgari
<sh...@webstep.no>wrote:

> Hi.
>
> I am very very new to Flex.
> I created this thread for sorting in Java
> http://stackoverflow.com/q/15091732/37298
>
> However, I am now trying to do the same thing in ActionScript.
>
> I have a Bindable ArrayCollection with Objects that looks like this
>
> Code.code : F
> Code.label: Foo
> Code.description: The Foo description
>
> Then I want to sort exactly as specified in the stackoverflow site.
> I have around 10 of them which I need in a particular order. I have them
> defined in an array.
>
> Based on the stackoverflow question and some googling, I have come up with
> the following:
>
> private function myCompare(a:Object, b:Object, fields:Array = null):int {
>             if (mostUsed.contains(a.code) && mostUsed.contains(b.code)) {
>                 return fields.getItemIndex(a) - fields.getItemIndex(b);
>             }
>
>             if (mostUsed.contains(a.code)) {
>                 return -1;
>             }
>
>             if (mostUsed.contains(b.code)) {
>                 return 1;
>             }
>
>             var s:Sort = new Sort();
>             s.fields = fields;
>             var f:Function = s.compareFunction;
>             return f.call(null,a,b,fields);
>         }
>
> private var mostUsed:ArrayCollection = new ArrayCollection(["F", "A", "B",
> "FTR", "HIS", "KEB", "LEG", "MED", "MER", "PEN", "PEO", "PER", "PIN",
> "PUR", "REF"]);
>
> var grunner:ArrayCollection = //getting collection
> grunner.sort.fields = [new SortField("code")];
> grunner.sort.compareFunction = myCompare;
> grunner.refresh();
>
> But it doesnt work. Any clues?
> My collection returns empty.
>
> Regards Shervin
>

Re: Specific sorting, then alphabetic

Posted by Shervin Asgari <sh...@webstep.no>.
Thanks all.

I have solved the issue.
The error seemed to be from this line: return fields.getItemIndex(a) -
fields.getItemIndex(b);
Somehow it didn't get the index right.

I changed it to do a normal alpabetic search in those cases, and it worked.

Shervin


2013/2/27 Justin Mclean <ju...@classsoftware.com>

> Hi,
>
> It may be theres an issue at this line unless your code you posted is
> incomplete:
> var grunner:ArrayCollection = //getting collection
>
> After this line is there anything in the grunner array?
>
> Add trace statements or run though a debugger to see what the issues is
> would be the simple way to find out what is wrong in the code.
>
> Thanks,
> Justin

Re: Specific sorting, then alphabetic

Posted by Justin Mclean <ju...@classsoftware.com>.
Hi,

It may be theres an issue at this line unless your code you posted is incomplete:
var grunner:ArrayCollection = //getting collection

After this line is there anything in the grunner array?

Add trace statements or run though a debugger to see what the issues is would be the simple way to find out what is wrong in the code.

Thanks,
Justin

Re: Specific sorting, then alphabetic

Posted by Shervin Asgari <sh...@webstep.no>.
If you look at here:
http://stackoverflow.com/questions/1368122/flex-sort-writing-a-custom-comparefunction

There they both use the Sort and compareFunction. Thats what I am trying to
accomplish. I am trying to do a custom sort, then alphabetic.


2013/2/27 Tom Chiverton <tc...@extravision.com>

> I meant
>
> grunner.sort.fields = [new SortField("code")];    // Sort
> grunner.sort.compareFunction = myCompare;    //compareFunction
>
> If your IDE has given up, just add some trace() statements. There's a
> method in ObjectUtil that'll dump Array (well, anything) sensibly.
>
> Tom
>
>
> On 27/02/2013 08:55, Shervin Asgari wrote:
>
>> What do you mean sort and compareFunction?
>>
>
>

Re: Specific sorting, then alphabetic

Posted by Tom Chiverton <tc...@extravision.com>.
I meant

grunner.sort.fields = [new SortField("code")];    // Sort
grunner.sort.compareFunction = myCompare;    //compareFunction

If your IDE has given up, just add some trace() statements. There's a 
method in ObjectUtil that'll dump Array (well, anything) sensibly.

Tom

On 27/02/2013 08:55, Shervin Asgari wrote:
> What do you mean sort and compareFunction?


Re: Specific sorting, then alphabetic

Posted by Shervin Asgari <sh...@webstep.no>.
What do you mean sort and compareFunction?
The reason why its empty is because for some reason an error occurs, and my
Intellij doesn't print out the error messages. So its difficult for me to
know exactly what went wrong.
The array is not empty, and the objects are not null.

Shervin


2013/2/27 Tom Chiverton <tc...@extravision.com>

> I wouldn't give a Sort and a compareFunction - what's the point ? Just
> have one or the other.
>
> Either way, this wont empty your ArrayCollection. If you dump it out
> before and after
>
> var grunner:ArrayCollection
>
> along with where ever it came from, are your *sure* it isn't being
> populated by an empty array, null object, failed cast etc. ?
>
> Failing that, make a standalone example we can all build.
>
> Tom
>
>
> On 27/02/2013 08:31, Shervin Asgari wrote:
>
>> Hi.
>>
>> I am very very new to Flex.
>> I created this thread for sorting in Java
>> http://stackoverflow.com/q/**15091732/37298<http://stackoverflow.com/q/15091732/37298>
>>
>> However, I am now trying to do the same thing in ActionScript.
>>
>> I have a Bindable ArrayCollection with Objects that looks like this
>>
>> Code.code : F
>> Code.label: Foo
>> Code.description: The Foo description
>>
>> Then I want to sort exactly as specified in the stackoverflow site.
>> I have around 10 of them which I need in a particular order. I have them
>> defined in an array.
>>
>> Based on the stackoverflow question and some googling, I have come up with
>> the following:
>>
>> private function myCompare(a:Object, b:Object, fields:Array = null):int {
>>              if (mostUsed.contains(a.code) && mostUsed.contains(b.code)) {
>>                  return fields.getItemIndex(a) - fields.getItemIndex(b);
>>              }
>>
>>              if (mostUsed.contains(a.code)) {
>>                  return -1;
>>              }
>>
>>              if (mostUsed.contains(b.code)) {
>>                  return 1;
>>              }
>>
>>              var s:Sort = new Sort();
>>              s.fields = fields;
>>              var f:Function = s.compareFunction;
>>              return f.call(null,a,b,fields);
>>          }
>>
>> private var mostUsed:ArrayCollection = new ArrayCollection(["F", "A", "B",
>> "FTR", "HIS", "KEB", "LEG", "MED", "MER", "PEN", "PEO", "PER", "PIN",
>> "PUR", "REF"]);
>>
>> var grunner:ArrayCollection = //getting collection
>> grunner.sort.fields = [new SortField("code")];
>> grunner.sort.compareFunction = myCompare;
>> grunner.refresh();
>>
>> But it doesnt work. Any clues?
>> My collection returns empty.
>>
>> Regards Shervin
>>
>>
>> ______________________________**______________________________**
>> __________
>> This email has been scanned by the Symantec Email Security.cloud service.
>> For more information please visit http://www.symanteccloud.com
>> ______________________________**______________________________**
>> __________
>>
>
>

Re: Specific sorting, then alphabetic

Posted by Tom Chiverton <tc...@extravision.com>.
I wouldn't give a Sort and a compareFunction - what's the point ? Just 
have one or the other.

Either way, this wont empty your ArrayCollection. If you dump it out 
before and after

var grunner:ArrayCollection

along with where ever it came from, are your *sure* it isn't being 
populated by an empty array, null object, failed cast etc. ?

Failing that, make a standalone example we can all build.

Tom

On 27/02/2013 08:31, Shervin Asgari wrote:
> Hi.
>
> I am very very new to Flex.
> I created this thread for sorting in Java
> http://stackoverflow.com/q/15091732/37298
>
> However, I am now trying to do the same thing in ActionScript.
>
> I have a Bindable ArrayCollection with Objects that looks like this
>
> Code.code : F
> Code.label: Foo
> Code.description: The Foo description
>
> Then I want to sort exactly as specified in the stackoverflow site.
> I have around 10 of them which I need in a particular order. I have them
> defined in an array.
>
> Based on the stackoverflow question and some googling, I have come up with
> the following:
>
> private function myCompare(a:Object, b:Object, fields:Array = null):int {
>              if (mostUsed.contains(a.code) && mostUsed.contains(b.code)) {
>                  return fields.getItemIndex(a) - fields.getItemIndex(b);
>              }
>
>              if (mostUsed.contains(a.code)) {
>                  return -1;
>              }
>
>              if (mostUsed.contains(b.code)) {
>                  return 1;
>              }
>
>              var s:Sort = new Sort();
>              s.fields = fields;
>              var f:Function = s.compareFunction;
>              return f.call(null,a,b,fields);
>          }
>
> private var mostUsed:ArrayCollection = new ArrayCollection(["F", "A", "B",
> "FTR", "HIS", "KEB", "LEG", "MED", "MER", "PEN", "PEO", "PER", "PIN",
> "PUR", "REF"]);
>
> var grunner:ArrayCollection = //getting collection
> grunner.sort.fields = [new SortField("code")];
> grunner.sort.compareFunction = myCompare;
> grunner.refresh();
>
> But it doesnt work. Any clues?
> My collection returns empty.
>
> Regards Shervin
>
>
> ______________________________________________________________________
> This email has been scanned by the Symantec Email Security.cloud service.
> For more information please visit http://www.symanteccloud.com
> ______________________________________________________________________