PDA

View Full Version : Sort Array Collection


NewToActionscript
11-14-2009, 10:29 PM
Hi,

I'm new to flex programming but have some exposure to basic programming, have downloaded code below which finds and displays fields in an attribute table of feature, seems to be loading this into array collection. The problem I have is that it displays it in a random order, and I need it displayed in the order the fields are in the table, or in alphabetical order. Tried to do a sort on the array collection but didn't work, any ideas would be really appreciated.

Many thanks,

[CODE]
<mx:Script>
<![CDATA[

import com.esri.ags.events.DrawEvent;
import com.esri.ags.events.IdentifyEvent;
import com.esri.ags.geometry.Extent;
import com.esri.ags.geometry.Geometry;
import com.esri.ags.geometry.MapPoint;
import com.esri.ags.geometry.Polygon;
import com.esri.ags.geometry.Polyline;
import com.esri.ags.Graphic;
import com.esri.ags.layers.ArcGISTiledMapServiceLayer;
import com.esri.ags.layers.ArcGISDynamicMapServiceLayer;
import com.esri.ags.layers.GraphicsLayer;
import com.esri.ags.layers.Layer;
import com.esri.ags.Map;
import com.esri.ags.symbol.PictureMarkerSymbol;
import com.esri.ags.symbol.SimpleFillSymbol;
import com.esri.ags.symbol.SimpleLineSymbol;
import com.esri.ags.tasks.FeatureSet;
import com.esri.ags.tasks.IdentifyParameters;
import com.esri.ags.tasks.IdentifyResult;
import com.esri.ags.tasks.IdentifyTask;
import com.esri.ags.tasks.Query;
import com.esri.ags.tasks.QueryTask;
import com.esri.ags.toolbars.Draw;
import com.esri.solutions.flexviewer.SiteContainer;
import com.esri.solutions.flexviewer.utils.WidgetEffects;
import mx.collections.*;
import mx.controls.Alert;
import mx.rpc.AsyncResponder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

//labels
private var identifyLabel:String;

private var resultsLabel:String;

[Bindable]
private var descriptionLabel:String;

private var identifyLayerOption:String;

private var identifyTolerance:Number = 3;

[Bindable]
private var pointLabel:String;

[Bindable]
private var clearLabel:String;

private var loadingLabel:String;

private var selectionLabel:String;

[Bindable]
private var msgVisible:Boolean = false;

private var zoomScale:Number = 5000;

private var graphicsLayer:GraphicsLayer;

private var graphicPointSym:PictureMarkerSymbol;

private var identifyPoint:MapPoint;

[Bindable]
private var identifyArrayCollection:ArrayCollection;

private const ICON_URL:String = "com/esri/solutions/flexviewer/assets/images/icons/";

private function init():void
{
graphicPointSym = new PictureMarkerSymbol(widgetIcon, 30, 30)
graphicsLayer = new GraphicsLayer();
graphicsLayer.symbol = graphicPointSym;
map.addLayer(graphicsLayer);
if (configXML)
{
//labels
identifyLabel = configXML.labels.graphicalsearchlabel || "Identify";
resultsLabel = configXML.labels.resultslabel || "Results";
descriptionLabel = configXML.labels.descriptionlabel || "Use the identify tool to identify features on the map:";
pointLabel = configXML.labels.pointlabel || "Identify";
clearLabel = configXML.labels.clearlabel || "Clear";
loadingLabel = configXML.labels.loadinglabel || "Loading...";
selectionLabel = configXML.labels.selectionlabel || "Features Selected:";

identifyLayerOption = configXML.identifylayeroption || IdentifyParameters.LAYER_OPTION_VISIBLE;
if (Number(configXML.identifytolerance) > 0)
identifyTolerance = Number(configXML.identifytolerance);
if (Number(configXML.zoomscale) > 0)
zoomScale = Number(configXML.zoomscale);
}

wTemplate.addTitlebarButton(ICON_URL + "i_table.png", resultsLabel, showStateResults);
wTemplate.addTitlebarButton(ICON_URL + "i_info.png", identifyLabel, showStateIdentify);
activateIdentifyTool();
}

private function activateIdentifyTool():void
{
var status:String = pointLabel;
setMapAction(Draw.MAPPOINT, status, drawEnd);;
}

private function drawEnd(event:DrawEvent):void
{
clear();
graphicsLayer.add(event.graphic);
identifyPoint = event.graphic.geometry as MapPoint;
identifyFeatures();
}

//identify features
private function identifyFeatures():void
{
var identifyParams : IdentifyParameters = new IdentifyParameters();
identifyParams.returnGeometry = false;
identifyParams.tolerance = identifyTolerance;
identifyParams.geometry = identifyPoint;
identifyParams.width = map.width;
identifyParams.height = map.height;
identifyParams.layerOption = identifyLayerOption;
identifyParams.mapExtent = map.extent;

for (var i:Number = map.layerIds.length -1; i >= 0; i--)
{
var layer:Layer = map.getLayer(map.layerIds[i]);
var url:String;
if (layer.visible)
{
if (layer is ArcGISDynamicMapServiceLayer)
{
var dynamicLayer:ArcGISDynamicMapServiceLayer = layer as ArcGISDynamicMapServiceLayer;
url = dynamicLayer.url;
}
else if (layer is ArcGISTiledMapServiceLayer)
{
var tiledLayer:ArcGISTiledMapServiceLayer = layer as ArcGISTiledMapServiceLayer;
url = tiledLayer.url;
}

if(url)
{
var identifyTask:IdentifyTask = new IdentifyTask(url);
identifyTask.addEventListener(IdentifyEvent.IDENTI FY_COMPLETE, onResult);
identifyTask.addEventListener(FaultEvent.FAULT, onFault);
identifyTask.execute(identifyParams);
showMessage(loadingLabel, true);
showStateResults(null);
}
}
}
}

// on result
private function onResult(event:IdentifyEvent):void
{
try
{
processIdentifyResults(event.identifyResults);
}
catch (error:Error)
{
showMessage(error.message, false);
}

}

//on fault
private function onFault(event:FaultEvent):void
{
showMessage(event.fault.faultDetail, false);
}

//proocess identify results
private function processIdentifyResults(identifyResults:Array):void
{
if(!identifyArrayCollection)
identifyArrayCollection = new ArrayCollection();
for each (var identifyResult:IdentifyResult in identifyResults)
{
var title:String = identifyResult.layerName;
var obj:Object = identifyResult.feature.attributes;
var content:String = "";
var fld:String;
var value:String;
for (fld in obj)
{
value = obj[fld].toString();
content += fld + ": " + value + "\n";
}
var infoData:Object =
{
icon: widgetIcon,
title: identifyResult.layerName,
content: content,
link: "",
point: identifyPoint,
geometry: identifyPoint
};
identifyArrayCollection.addItem(infoData);
}
clearMessage();
}

private function clear():void
{
graphicsLayer.clear();
clearMessage();
identifyArrayCollection = null;
identifyPoint = null;
this.showInfoWindow(null);
}

private var hitimer:uint;

private function mouseOverRecord(event:MouseEvent):void
{
var infoData:Object = event.currentTarget.infoData;
clearTimeout(hitimer);
hitimer = setTimeout(showHighlight, 800, [infoData]);
}

private function mouseOutRecord():void
{
clearTimeout(hitimer);
this.showInfoWindow(null);
}

private function clickRecord(event:MouseEvent):void
{
var infoData:Object = event.currentTarget.infoData;
var pt:MapPoint = infoData.point;
if (map.scale > zoomScale)
map.scale = zoomScale;
map.centerAt(pt);
}

private function showHighlight(params:Array):void
{
var infoData:Object = params[0];
var pt:MapPoint = infoData.point;
//if (!map.extent.contains(pt))
// map.centerAt(pt);
this.showInfoWindow(params[0]);
}

private function showMessage(msg:String, swfVisible:Boolean):void
{
txtMessage.text = msg;
swfMessage.visible = swfVisible;
msgVisible = true;
}

private function clearMessage():void
{
msgVisible = false;
}

private function widgetClosedHandler(event:Event):void
{
graphicsLayer.visible = false;
this.showInfoWindow(null);
setMapNavigation(null, null);
}

private function widgetOpenedHandler(event:Event):void
{
activateIdentifyTool();
graphicsLayer.visible = true;
}

private function showStateIdentify(event:MouseEvent):void
{
WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 0, 400);
}

private function showStateResults(event:MouseEvent):void
{
WidgetEffects.flipWidget(this, viewStack, "selectedIndex", 1, 400);
}

]]>
</mx:Script>

[CODE]

suchislife801
11-15-2009, 07:12 AM
Sorting an ArrayCollection using the SortField and Sort classes

http://blog.flexexamples.com/2007/08/05/sorting-an-arraycollection-using-the-sortfield-and-sort-classes/

Specifically this part...

/** This method gets called by the Button control's click handler and creates a new SortField and Sort object which are used to sort the ArrayCollection. */
private function button_click():void {
/* Create the SortField object for the "data" field in the ArrayCollection object, and make sure we do a numeric sort. */
var dataSortField:SortField = new SortField();
dataSortField.name = "data";
dataSortField.numeric = true;

/* Create the Sort object and add the SortField object created earlier to the array of fields to sort on. */
var numericDataSort:Sort = new Sort();
numericDataSort.fields = [dataSortField];

/* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */
arrColl.sort = numericDataSort;
arrColl.refresh();
}

NewToActionscript
11-15-2009, 08:59 AM
Hi
thanks for the reply, I tried to use that code and put it in as a separate method that was called after the processIdentifyResults method was called but I got an error message that seems to indicate that there is no data in the field to search on, so I was wondering if I'm calling it at the wrong time or if I just haven't gotten it implemented correctly? I've included the relevant bits of the code below

Thanks!

;

/* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */
identifyArrayCollection.sort = numericDataSort;
identifyArrayCollection.refresh();
}

suchislife801
11-15-2009, 07:50 PM
This is a guess but, I think you must change the name of the field we are sorting on the sample I posted for the name of your field which is more then likely different. Suppouse the ArrayCollection field you are trying to sort is called myField.

var dataSortField:SortField = new SortField();
dataSortField.name = "data"; // <-- Change "data" for "myField".
dataSortField.numeric = false; // <-- false = string | true = number

gautamsvas
12-16-2009, 09:47 AM
var dataSortField:SortField = new SortField();
dataSortField.name = "data";
dataSortField.numeric = true;

/* Create the Sort object and add the SortField object created earlier to the array of fields to sort on. */
var numericDataSort:Sort = new Sort();
numericDataSort.fields = [dataSortField];

/* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */
arrColl.sort = numericDataSort;
arrColl.refresh();
this is not working to sort a column in datagrid onload.can someone plz help

marco.rossi
12-16-2009, 04:40 PM
Could you please try:

var dataSortField:SortField = new SortField();
dataSortField.name = "title";
dataSortField.numeric = false;

/* Create the Sort object and add the SortField object created earlier to the array of fields to sort on. */
var numericDataSort:Sort = new Sort();
numericDataSort.fields = [dataSortField];

/* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */
identifyArrayCollection.sort = numericDataSort;
identifyArrayCollection.refresh();

I'm taking a wild gues, and assume that the array you want sorted is identifyArrayCollection.

If this does not work, please post the error message too.

Marco.