Flex arraycollection sorting not working

孤街醉人 提交于 2019-12-12 02:44:15

问题


I am trying to sort a list of string stored in an arraycollection. But the sorted result is not correct. Please see my code.

spark.collections.Sort

if(value is ArrayCollection){
            var sort:Sort=new Sort();
            var sortField:SortField = new SortField("data")
            sortField.numeric=false;
            sort.fields=[sortField];

            ArrayCollection(value).sort=sort;
            ArrayCollection(value).refresh();
        }

Input: Start With, Contains, End With, Equals IgnoreCase, Not Equals, Matching, Equals

Output: Equals IgnoreCase, Contains, End With, Start With, Not Equals, Matching, Equals

Some time only one row is swapping with another(as above), some time no sorting at all.


回答1:


In case of your array collection having list of string. you need not specify name of SortField your case data.

            var value:ArrayCollection = new ArrayCollection(['Start With','Contains','End With','Equals IgnoreCase','Not Equals','Equals']);
            var dataSortField:SortField = new SortField(); //Leave it empty.
            dataSortField.numeric = false;

            var dataSort:Sort = new Sort();
            dataSort.fields=[dataSortField];

            value.sort = dataSort;
            value.refresh();

o/p:

   "value"  mx.collections.ArrayCollection (@31ced61)   
[0] "Contains"  
[1] "End With"  
[2] "Equals"    
[3] "Equals IgnoreCase" 
[4] "Not Equals"    
[5] "Start With"

If arraycollection having object with data property your code is absolutly correct. like

            var value:ArrayCollection = new ArrayCollection();
            value.addItem({data:'Start With'});
            value.addItem({data:'Contains'});
            value.addItem({data:'End With'});
            value.addItem({data:'Equals IgnoreCase'});
            value.addItem({data:'Not Equals'});
            value.addItem({data:'Equals'});

This case you need to specify like

var sortField:SortField = new SortField("data");


来源:https://stackoverflow.com/questions/13857369/flex-arraycollection-sorting-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!