Sorting 在Flex中使用两个字段对数组集合排序

Sorting 在Flex中使用两个字段对数组集合排序,sorting,actionscript-3,apache-flex,flex4,flex4.5,Sorting,Actionscript 3,Apache Flex,Flex4,Flex4.5,我有一个数组集合: private var arrayCollection:ArrayCollection = new ArrayCollection([ {Type:L, Name:"DDD"}, {Type:H, Name:"EEE"}, {Type:H, Name:"AAA"}, {Type:L, Name:"AFG"}, {Type:L, Name:"ZZZ"}, {Type:H, Name:

我有一个数组集合:

private var arrayCollection:ArrayCollection = new ArrayCollection([
        {Type:L, Name:"DDD"},
        {Type:H, Name:"EEE"},
        {Type:H, Name:"AAA"},
        {Type:L, Name:"AFG"},
        {Type:L, Name:"ZZZ"},
        {Type:H, Name:"XXX"},
        {Type:, Name:"DeSelectAll"},
        {Type:G, Name:"NNN"},
        {Type:L, Name:"CCC"},
        {Type:, Name:"SelectAll"},
        {Type:H, Name:"BBB"},
        {Type:H, Name:"HHH"},
        {Type:L, Name:"LLL"}
    ]);
我想使用这两个字段Type和Name对数组集合进行排序。首先,所有记录都将为“H”类型,并带有名称排序。之后,所有记录都将为“L”类型,并带有名称排序,在列表顶部的“G”和“DeceleAll”类型之后,以及在“DeceleAll”之后的“SelectAll”。我想要这样的结果

Name:"DeSelectAll" Type:""
Name:"SelectAll" Type:""   
Name:AAA Type: H
Name:BBB Type: H
Name:EEE Type: H
Name:HHH Type: H
Name:XXX Type: H
Name:AFG Type: L
Name:CCC Type: L
Name:DDD Type: L
Name:LLL Type: L
Name:ZZZ Type: L
Name:NNN Type: G

请使用sort()为其提供一些代码。

因此,基本上,您希望先按类型排序,然后按名称排序,应该使用以下内容:

var nameSortField:SortField = new SortField("Name");
var typeSortField:SortField = new SortField("Type");
arrayCollection.sort = new Sort();
arrayCollection.sort.fields = [typeSortField, nameSortField];
arrayCollection.refresh();
编辑:

如果您需要类型属性的自定义顺序(因此“G”位于末尾),则需要类型字段的自定义比较函数:

// This is the sort order for your Type. First empty, then H, then L and then G
// It is not clear if G, H and L are properties in your code. If that are properties you should remove the quotes around G, H and L
private var typeOrder:Array = ["","H", "L", "G"];

var nameSortField:SortField = new SortField("Name");
var typeSortField:SortField = new SortField("Type");
typeSortField.compareFunction = typeCompareFunction;

arrayCollection.sort = new Sort();
arrayCollection.sort.fields = [typeSortField, nameSortField]
arrayCollection.refresh();

private function typeCompareFunction(a:Object, b:Object, fields:Array = null):int
{    
    if (typeOrder.indexOf(a.Type) < typeOrder.indexOf(b.Type))
    {
        // if the a.Type position in the array is before b.Type position the item a should be before b
        return -1;
    }
    else if (typeOrder.indexOf(a.Type) > typeOrder.indexOf(b.Type))
    {
        // if the a.Type position in the array is after b.Type position the item a should be after b
        return 1;
    }
    else
    {
        return 0;
    }
}
//这是您的类型的排序顺序。首先是空的,然后是H,然后是L,然后是G
//不清楚G、H和L是否是代码中的属性。如果这是属性,则应删除G、H和L周围的引号
私有变量类型顺序:数组=[“”,“H”,“L”,“G”];
var nameSortField:SortField=新的SortField(“名称”);
变量typeSortField:SortField=新的SortField(“类型”);
typeSortField.compareFunction=typeCompareFunction;
arrayCollection.sort=新排序();
arrayCollection.sort.fields=[typeSortField,nameSortField]
arrayCollection.refresh();
私有函数typeCompareFunction(a:Object,b:Object,fields:Array=null):int
{    
if(typeOrder.indexOf(a.Type)typeOrder.indexOf(b.Type))
{
//如果数组中a.Type位置在b.Type位置之后,则项目a应在b之后
返回1;
}
其他的
{
返回0;
}
}

因此,基本上,您希望先按类型排序,然后按名称排序,应该使用以下内容:

var nameSortField:SortField = new SortField("Name");
var typeSortField:SortField = new SortField("Type");
arrayCollection.sort = new Sort();
arrayCollection.sort.fields = [typeSortField, nameSortField];
arrayCollection.refresh();
编辑:

如果您需要类型属性的自定义顺序(因此“G”位于末尾),则需要类型字段的自定义比较函数:

// This is the sort order for your Type. First empty, then H, then L and then G
// It is not clear if G, H and L are properties in your code. If that are properties you should remove the quotes around G, H and L
private var typeOrder:Array = ["","H", "L", "G"];

var nameSortField:SortField = new SortField("Name");
var typeSortField:SortField = new SortField("Type");
typeSortField.compareFunction = typeCompareFunction;

arrayCollection.sort = new Sort();
arrayCollection.sort.fields = [typeSortField, nameSortField]
arrayCollection.refresh();

private function typeCompareFunction(a:Object, b:Object, fields:Array = null):int
{    
    if (typeOrder.indexOf(a.Type) < typeOrder.indexOf(b.Type))
    {
        // if the a.Type position in the array is before b.Type position the item a should be before b
        return -1;
    }
    else if (typeOrder.indexOf(a.Type) > typeOrder.indexOf(b.Type))
    {
        // if the a.Type position in the array is after b.Type position the item a should be after b
        return 1;
    }
    else
    {
        return 0;
    }
}
//这是您的类型的排序顺序。首先是空的,然后是H,然后是L,然后是G
//不清楚G、H和L是否是代码中的属性。如果这是属性,则应删除G、H和L周围的引号
私有变量类型顺序:数组=[“”,“H”,“L”,“G”];
var nameSortField:SortField=新的SortField(“名称”);
变量typeSortField:SortField=新的SortField(“类型”);
typeSortField.compareFunction=typeCompareFunction;
arrayCollection.sort=新排序();
arrayCollection.sort.fields=[typeSortField,nameSortField]
arrayCollection.refresh();
私有函数typeCompareFunction(a:Object,b:Object,fields:Array=null):int
{    
if(typeOrder.indexOf(a.Type)typeOrder.indexOf(b.Type))
{
//如果数组中a.Type位置在b.Type位置之后,则项目a应在b之后
返回1;
}
其他的
{
返回0;
}
}

这不是代码传递服务。显示您已尝试的内容,我们将帮助您修复错误/bug。这是一个硬编码数组集合。我只想把它分类。首先,所有记录都将是带有名称排序的“H”类型,然后所有记录都将是带有名称排序的“L”类型。这样我可以在排序后在下拉列表中显示。这不是代码传递服务。显示您已尝试的内容,我们将帮助您修复错误/bug。这是一个硬编码数组集合。我只想把它分类。首先,所有记录都将是带有名称排序的“H”类型,然后所有记录都将是带有名称排序的“L”类型。这样我可以在排序后将其显示在下拉列表中。谢谢,它工作得很好!!。但我已经修改了这个问题。请提供基于该场景的解决方案。它将以相同的方式工作-由于类型为空,选择/取消选择将位于所有其他内容之上。可能您需要在数组声明中将其设置为空字符串(类型:“”),那么类型:G应该在最后显示。谢谢它工作正常!!。但我已经修改了这个问题。请提供基于该场景的解决方案。它将以相同的方式工作-由于类型为空,选择/取消选择将位于所有其他内容之上。可能您需要在数组声明中将其设置为空字符串(类型:“”),那么类型:G应该在最后显示。