2009-10-21 12 views
10

C'è un modo in cui Flex è in grado di ordinare una raccolta di array basata su stringhe.Ordinamento di una serie di array in Flex

Ho un dataprovider con stringhe come "Critico" "Alto" "Medio" "Basso" dove in Ho bisogno di ordinarlo in modo tale che ho bisogno di Critical per essere visualizzato in alto e in alto Alto, Medio e Low segue.

Qualcuno può farmi sapere qualsiasi logica.

Grazie, Kumar

risposta

23

ArrayCollection è una sottoclasse di ListCollectionView che ha un sortproperty. La classe Sort ha un compareFunctionproperty che è possibile utilizzare per definire le funzioni di ordinamento personalizzate.

private function sortFunction(a:Object, b:Object, array:Array = null):int 
{ 
    //assuming that 'level' is the name of the variable in each object 
    //that holds values like "Critical", "High" etc 
    var levels:Array = ["Low", "Medium", "High", "Critical"]; 
    var aLevel:Number = levels.indexOf(a.level); 
    var bLevel:Number = levels.indexOf(b.level); 
    if(aLevel == -1 || bLevel == -1) 
     throw new Error("Invalid value for criticality "); 
    if(aLevel == bLevel) 
     return 0; 
    if(aLevel > bLevel) 
     return 1; 
    return -1; 
} 
var sort:Sort = new Sort(); 
sort.compareFunction = sortFunction; 
arrayCollection.sort = sort; 
arrayCollection.refresh(); 
+0

Funziona bene amar grazie – skumarvarma

+1

funzione dovrebbe restituire int – zinovii

+0

@zdmytriv fissi; Grazie :) – Amarghosh

Problemi correlati