How to sort a selection set

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

How to sort a selection set

soudemans
Hi,

I have a select type list box "resultSelect" that is populated from a MapGuide OSGeo selection set using the code below.  While doing some testing I noticed that the select box remained unsorted.  I did some searching to find an answer but my efforts came up empty.  I tried several things including things like results.sort() but no-go.  Does anyone have any idea how to sort the this set?

Thanks

asp
            <select class="Ctrl" id="resultSelect" size="15" onchange="OnResultChange()" style="width: 100%"></select>

javascript to populate the select box
        function OnReadyStateChange()
        {
            var ready = queryReqHandler.readyState;
            if (ready == READY_STATE_COMPLETE)
            {
                results = queryReqHandler.responseText.parseJSON();
                var resultSelect = document.getElementById("resultSelect");
                resultSelect.options.length = 0;
                for (var i = 0; i < results.length; i++)
                {
                    resultSelect.options[i] = new Option(results[i].displayValue, i, false, false);
                }
                OnResultChange();

                document.getElementById("executeBtn").disabled = false;
                document.getElementById("busyImg").src = NOT_BUSY_IMAGE;
                queryReqHandler = null;
            }
        }

Javascript to capture the returned selection set  "queryReqHandler"        function ExecuteQuery()
        {
            reqParams = "COMMAND=<%=Command.EXECUTEQUERY%>";
            reqParams += "&SESSION=" + encodeURIComponent(session);
            reqParams += "&MAPNAME=" + encodeURIComponent(mapName);
            reqParams += "&LAYERNAME=Sanitary%20Manholes";
            reqParams += "&USEPROPERTYFILTER=true";
            reqParams += "&PROPERTYNAME=MH_NO";
            reqParams += "&ISSTRING=true";
            reqParams += "&OPERATOR=1";
            reqParams += "&VALUE=" + encodeURIComponent(document.getElementById("valueInput").value);
            reqParams += "&USESPATIALFILTER=false";
            reqParams += "&OUTPUTPROPERTY=MH_NO";
            reqParams += "&QUERYMAX=" + encodeURIComponent(document.getElementById("queryMax").value);

            if (msie)
                queryReqHandler = new ActiveXObject("Microsoft.XMLHTTP");
            else
                queryReqHandler = new XMLHttpRequest();

            queryReqHandler.onreadystatechange = OnReadyStateChange;
            queryReqHandler.open("POST", "querycontroller.aspx", true);
            queryReqHandler.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            document.getElementById("executeBtn").disabled = true;
            document.getElementById("busyImg").src = BUSY_IMAGE;

            queryReqHandler.send(reqParams);
        }
Reply | Threaded
Open this post in threaded view
|

Re: How to sort a selection set

Jackie Ng
A javascript Array.sort() on an array of objects generally requires a comparer function be passed in that determines how one object is greater or less than another.

http://www.javascriptkit.com/javatutors/arraysort.shtml

- Jackie
Reply | Threaded
Open this post in threaded view
|

RE: How to sort a selection set

soudemans

Thanks Jackie,

 

But according to the information you provided I should be able to simply use the sort() method since I “just“ want  to alphabetically sort a string key field.  Thus I am wondering what is missing since that doesn’t work. Perhaps the array returned by MapGuide is not some ordinary array.  Have you done something like this, if so could you share how?

 

Sierk

 


If you reply to this email, your message will be added to the discussion below:

http://osgeo-org.1560.n6.nabble.com/How-to-sort-a-selection-set-tp4344383p4347394.html

To unsubscribe from How to sort a selection set, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

RE: How to sort a selection set

Jackie Ng
But what you are sort()'ing is an array of JSON objects right? That's why you must pass a sorting function as a parameter to sort() because otherwise sort() has no clue how the objects are to be sorted.

If it's an array of string literals, then sort() should do the alphabetic sort

- Jackie
Reply | Threaded
Open this post in threaded view
|

RE: How to sort a selection set

soudemans

Hmm, hadn’t thought about it in that light.  But it seems you are correct, I am sorting an array of JSON objects

 

Alright so I tried my hand at this, and to be honest it is a little over my head.  I found a JavaScript function that I could use to sort the values in the Array.  It looks like this:

 

var sort_by = function(field, reverse, primer){

     var key = function (x) {return primer ? primer(x[field]) : x[field]};

          return function (a,b) {

                var A = key(a), B = key(b);

                return ((A < B) ? -1 : (A > B) ? +1 : 0) * [-1,1][+!!reverse];

    }

};

 

And the suggested usage would be something like this:
// Provided example: Sort by city, case-insensitive, A-Z 
homes.sort(sort_by(
'city', false, function(a){return a.toUpperCase()})); 

 

The following is what I basically put together.  Unfortunately I have no clue what the primer is supposed to do so I went with the previous example since it didn’t matter if the main_ID was lower or upper case.  Alas it isn’t working and I am not sure what is wrong or where to go from here. 

 

results = results.sort(sort_by('MAIN_ID',false,function(a){return a.toUpperCase()}));

 

Any thoughts?

 


If you reply to this email, your message will be added to the discussion below:

http://osgeo-org.1560.n6.nabble.com/How-to-sort-a-selection-set-tp4344383p4350951.html

To unsubscribe from How to sort a selection set, click here.
NAML

Reply | Threaded
Open this post in threaded view
|

RE: How to sort a selection set

Jackie Ng
The sorting function need not be that complicated.

Something like this will suffice:

results.sort(function(objA, objB) {
   if (objA.SORT_PROPERTY < objB.SORT_PROPERTY) {
       return -1; //Less than
   } else if (objA.SORT_PROPERTY > objB.SORT_PROPERTY) {
       return 1; //Greater than
   } else {
       return 0; //Equal
   }
});

Where SORT_PROPERTY is the name of the property of the object you want to sort on.

- Jackie
Reply | Threaded
Open this post in threaded view
|

RE: How to sort a selection set

soudemans

Jackie,

 

Hey, thanks, I think I am making progress.  I am imagining that SORT_PROPERTY is supposed to be the field name of the passed data, but that does not work.  Can you clarify what I am supposed to use here, and if it is the field name how I can get determine what it is?

 

From: Jackie Ng [via OSGeo.org] [mailto:[hidden email]]
Sent: Tuesday, January 31, 2012 8:21 AM
To: Oudemans, Sierk
Subject: RE: How to sort a selection set

 

The sorting function need not be that complicated.

Something like this will suffice:

results.sort(function(objA, objB) {
   if (objA.SORT_PROPERTY < objB.SORT_PROPERTY) {
       return -1; //Less than
   } else if (objA.SORT_PROPERTY > objB.SORT_PROPERTY) {
       return 1; //Greater than
   } else {
       return 0; //Equal
   }
});

Where SORT_PROPERTY is the name of the property of the object you want to sort on.

- Jackie


If you reply to this email, your message will be added to the discussion below:

http://osgeo-org.1560.n6.nabble.com/How-to-sort-a-selection-set-tp4344383p4353253.html

To unsubscribe from How to sort a selection set, click here.
NAML