【问题标题】:ActionScript: How to sort a class vector based on intActionScript:如何根据 int 对类向量进行排序
【发布时间】:2015-09-03 11:41:29
【问题描述】:

我有一个 TestClass 对象的向量,我想根据每个 TestClass 对象的整数升序排列。我该怎么做?

主要方法:

public class testSave extends Sprite
    {
        public function testSave()
        {
            var testVector:Vector.<TestClass> = new Vector.<TestClass>;

            testVector.push(new TestClass(5, "Testing"), new TestClass(2, "HelloWorld"), new TestClass(7, "Ohai");


        }
    }

测试类

public class TestClass
{
    public function TestClass(testi:int, tests:String)
    {
        this.stest = tests;
        this.itest = testi
    }

    public var stest:String;
    public var itest:int;


}

【问题讨论】:

    标签: class vector actionscript integer


    【解决方案1】:

    不幸的是,Vectors 没有 sortOn,但有一个排序。所以如果你这样做:

    public function testSave():void {
      var testVector:Vector.<TestClass> = new Vector.<TestClass>;
    
      testVector.push(new TestClass(5, "Testing"), new TestClass(2, "HelloWorld"), new TestClass(7, "Ohai"));
    
      testVector.sort(function(a:TestClass, b:TestClass):Boolean {
        return a.itest > b.itest;
      });
    
      // confirm the vector is now sorted
      for (var i = 0; i < testVector.length; i++){
        trace(testVector[i].itest);
      }
    }
    

    您的矢量将排序。 Vector.sort 采用排序函数,它比较两个值,然后在遍历数组时计算出如何交换它们...

    检查a是否大于b将导致升序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 1970-01-01
      • 1970-01-01
      • 2015-06-22
      • 2020-08-03
      • 1970-01-01
      相关资源
      最近更新 更多