Flex 按内部类对数组集合进行排序

发布于 2024-12-05 08:21:51 字数 1239 浏览 0 评论 0原文

我想以一种我不确定是否可行的方式对数组集合进行排序。

通常当你想要排序时,你会遇到这样的事情。

var dataSortField1:SortField = new SortField();
dataSortField1.name = fieldOneToSortBy;
dataSortField1.numeric = fieldOneIsNumeric;

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

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

所以如果我有一堂课,

public class ToSort {
    public var int:a
}

我可以输入

var ts1:ToSort = new ToSort();  
ts1.a = 10;
var ts2:ToSort = new ToSort();  
ts2.a = 20;
var arrayCollection:ArrayCollection = new ArrayCollection([ts1, ts2])
var dataSortField1:SortField = new SortField();
dataSortField1.name = "a";
dataSortField1.numeric = true;

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

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

“这很好”。我的问题是我现在继承了一个类,其中包含另一个类,我也需要对此进行排序。 例如

public class ToSort2 {
    public var int:a
    public var ToSortInner: inner1
}

public class ToSortInner {
    public var int:aa
    public var int:bb
}

,如果 a 在多个类中相同,那么我想对 ToSortInner2.aa 进行排序这可能吗?我尝试传入inner1.aa 作为排序字段名称,但这不起作用。

希望这一点是清楚的。如果没有,我会发布更多代码。

谢谢

I want to sort an array collection in a way that I am not sure is possible.

Usually when you want to sort you have something like this.

var dataSortField1:SortField = new SortField();
dataSortField1.name = fieldOneToSortBy;
dataSortField1.numeric = fieldOneIsNumeric;

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

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

so if I had a class

public class ToSort {
    public var int:a
}

I could type

var ts1:ToSort = new ToSort();  
ts1.a = 10;
var ts2:ToSort = new ToSort();  
ts2.a = 20;
var arrayCollection:ArrayCollection = new ArrayCollection([ts1, ts2])
var dataSortField1:SortField = new SortField();
dataSortField1.name = "a";
dataSortField1.numeric = true;

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

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

This works fine. My problem is I now have inherited a class that has another class inside it and I need to sort against this as well.
For example

public class ToSort2 {
    public var int:a
    public var ToSortInner: inner1
}

public class ToSortInner {
    public var int:aa
    public var int:bb
}

if a is the same in multiple classes then I want to sort on ToSortInner2.aa Is this possible. I have tried to pass in inner1.aa as the sort field name but this does not work.

Hope this is clear. If not I'll post some more code.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

世态炎凉 2024-12-12 08:21:51

您需要编写自定义排序 compareFunction< /a>.您可以深入了解函数内的对象属性。

从概念上讲是这样的:

public function aSort(a:Object, b:Object, fields:Array = null):int{
  if(a.aa is b.aa){
    return 0
  } else if(a.aa > b.aa) {
    return 1
  } else{
    return -1
  }
}

当您创建排序对象时,您可以指定比较函数:

var dataSort:Sort = new Sort();
dataSort.compareFunction = aSort;

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

You need to write a custom sort compareFunction. You can drill down into the objects properties inside the function.

Conceptually something like this:

public function aSort(a:Object, b:Object, fields:Array = null):int{
  if(a.aa is b.aa){
    return 0
  } else if(a.aa > b.aa) {
    return 1
  } else{
    return -1
  }
}

When you create your sort object, you can specify the compare function:

var dataSort:Sort = new Sort();
dataSort.compareFunction = aSort;

arrayCollection.sort = dataSort;
arrayCollection.refresh();
奢望 2024-12-12 08:21:51

Sort还有另一个属性,compareFunction。

Sort also has another property, compareFunction.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文