2012-11-23 256 views
0

我需要以下結果。使用自定義compareFunction對數據網格進行Flex排序(排序數字和文本字母數字)

所有數字都使用數字排序,所有字符串都使用字母數字排序。進一步,該數值應的字符串值之前所列:

實施例: 之前的 「i」, 「9」, 「89」, 「0045」, 「B」,在 「×」

「9」,「0045」,「89」,「b」,「i」,「x」

我目前的代碼如下所示:(數字排序工作,但我的字符串分佈到頂部和底部? - > 「b」, 「X」, 「9」, 「0045」, 「89」, 「我」)

public function compareFunction(obj1:Object, obj2:Object):int { 
    var id1:String = (obj1 as WdProblem).id; 
    var id2:String = (obj2 as WdProblem).id; 

    if(id1.replace(' ', '') == "n") { 
    var sdld:int = 0; 
    } 

    var num1:int = Number(id1); 
    var num2:int = Number(id2); 

    if(stringIsAValidNumber(id1) && stringIsAValidNumber(id2)) { 
    if(num1 == num2) { 
     return 0; 
    } else { 
     if(num1 > num2) { 
     return 1; 
     } else { 
     return -1; 
     } 
    } 
    } else if(!stringIsAValidNumber(id1) && !stringIsAValidNumber(id2)) { 
    return ObjectUtil.compare(id1, id2); 
     //return compareString(id1, id2); 

    } else if(!stringIsAValidNumber(id1) && stringIsAValidNumber(id2)) { 
    return 1; 
    } else if(stringIsAValidNumber(id1) && !stringIsAValidNumber(id2)) { 
    return -1; 
    } 

    return -1; 
} 

private function stringIsAValidNumber(s:String):Boolean { 
    return Boolean(s.match("[0-9]+(\.[0-9][0-9]?)?")); 
} 
+0

也許你要一點點前言添加到您的問題提供一些背景是什麼你正在努力。 – JeffryHouser

回答

0

我的建議是把它分解成它的組成部分,特別是如果你有一個風光類似這樣的一個或多個優先級排序。關鍵是隻有當第一次排序返回時它們相等,才能進入下一個排序。

對於您的情況,我會構建2種,一種用於數字,另一種用於字母數字,然後您的主排序可以通過調用子排序來優先化這些排序。

比如我有類似的東西:

private function sort2DimensionsByIncomeVsTime(a:OLAPSummaryCategory, b:OLAPSummaryCategory, fields:Array = null):int 
{ 
    var sort:OLAPSort = new OLAPSort(); 
    var incomeSort:int = sort.sortIncome(a, b); 
    var nameSort:int = sort.sortName(a, b); 
    var yrSort:int = sort.sortYear(a, b); 
    var moSort:int = sort.sortMonth(a, b); 

    if (incomeSort == 0) 
    { 
     if (nameSort == 0) 
     { 
      if (yrSort == 0) 
      { 
       //trace(a.name, a.year, a.month, 'vs:', b.name, b.year, b.month, 'month sort:', moSort); 
       return moSort; 
      } 
      else return yrSort; 
     } 
     else return nameSort; 
    } 
    else return incomeSort; 
} 
0

我用下面的排序功能字母數字排序:

<?xml version="1.0" encoding="utf-8"?> 
<s:Application 
    creationComplete="onCC()" 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"> 

    <fx:Script> 
     <![CDATA[ 
      import mx.collections.ArrayCollection; 
      import mx.utils.ObjectUtil; 
      import mx.utils.StringUtil; 

      import spark.collections.Sort; 
      import spark.collections.SortField; 

      public function onCC():void 
      { 
       var acAlphaNumericString:ArrayCollection = new ArrayCollection(); 
       acAlphaNumericString.addItem({ name: "NUM10071" }); 
       acAlphaNumericString.addItem({ name: "NUM9999" }); 
       acAlphaNumericString.addItem({ name: "9997" }); 
       acAlphaNumericString.addItem({ name: "9998" }); 
       acAlphaNumericString.addItem({ name: "9996" }); 
       acAlphaNumericString.addItem({ name: "9996F" }); 
       acAlphaNumericString.addItem({ name: "i" }); 
       acAlphaNumericString.addItem({ name: "9" }); 
       acAlphaNumericString.addItem({ name: "89" }); 
       acAlphaNumericString.addItem({ name: "0045" }); 
       acAlphaNumericString.addItem({ name: "b" }); 
       acAlphaNumericString.addItem({ name: "x" }); 

       var sf:SortField = new SortField("name"); 
       sf.compareFunction = function(o1:Object, o2:Object):int 
       { 
        return compare(o1.name, o2.name); 
       } 
       var sort:Sort = new Sort(); 
       sort.fields = [ sf ]; 
       acAlphaNumericString.sort = sort; 
       acAlphaNumericString.refresh(); 

       for each (var o:Object in acAlphaNumericString) 
        trace(o.name); 
      } 

      public static function compare(firstString:String, secondString:String):int 
      { 
       if (secondString == null || firstString == null) 
        return 0; 
       var lengthFirstStr:int = firstString.length; 
       var lengthSecondStr:int = secondString.length; 
       var index1:int = 0; 
       var index2:int = 0; 
       while (index1 < lengthFirstStr && index2 < lengthSecondStr) 
       { 
        var ch1:String = firstString.charAt(index1); 
        var ch2:String = secondString.charAt(index2); 
        var space1:String = ""; 
        var space2:String = ""; 
        do 
        { 
         space1 += ch1; 
         index1++; 
         if (index1 < lengthFirstStr) 
          ch1 = firstString.charAt(index1); 
         else 
          break; 
        } while (isDigit(ch1) == isDigit(space1.charAt(0))); 
        do 
        { 
         space2 += ch2; 
         index2++; 
         if (index2 < lengthSecondStr) 
          ch2 = secondString.charAt(index2); 
         else 
          break; 
        } while (isDigit(ch2) == isDigit(space2.charAt(0))); 

        var str1:String = new String(space1); 
        var str2:String = new String(space2); 
        var result:int; 
        if (isDigit(space1.charAt(0)) && isDigit(space2.charAt(0))) 
        { 
         var firstNumberToCompare:int = parseInt(StringUtil.trim(str1)); 
         var secondNumberToCompare:int = parseInt(StringUtil.trim(str2)); 
         result = ObjectUtil.numericCompare(firstNumberToCompare, secondNumberToCompare); 
        } 
        else 
         result = ObjectUtil.compare(str1, str2); 

        if (result != 0) 
         return result; 
       } 
       return lengthFirstStr - lengthSecondStr; 

       function isDigit(ch:String):Boolean 
       { 
        var code:int = ch.charCodeAt(0); 
        return code >= 48 && code <= 57; 
       } 
      } 
     ]]> 
    </fx:Script> 
</s:Application> 
相關問題