Link to home
Start Free TrialLog in
Avatar of Isaac
IsaacFlag for United States of America

asked on

sort Multi-dimensional array

I am trying to sort a multi-dimensional array.  In my code, I capture data (item 'ID' and 'Priority' value) from my SharePoint list and dump it into an array as objects.  For some odd reason, it does not get inserted in the order I expected so I am trying to sort the array by the 'ID'.
Here's my code that created the array of objects.
/*Array will contain two values, item id and new priority value. Global so it can be used in multiple functions.*/
var items = [];
var itemsID = [];
var projItemArray = new Array();
var i = 0;	

/*-------------------------------------------------------------------------------------------------------*/
//Success on data retrieval from 'retrieveListItems()'. Transforms HTML table to datatable and creates
//range functionality.
/*-------------------------------------------------------------------------------------------------------*/		
function onQuerySucceeded(sender, args) {
    var listItemInfo = '';
    var listItemEnumerator = collListItem.getEnumerator();

    //Will be used to build object array of ID and priorityNumber
    //var projItemArray = new Array();    	

    //1st call to retrieve the level. 2nd call below while loop
    var levels = determineGroup();
    // alert("onQuery: "+levels);

    var i = 0;
    while (listItemEnumerator.moveNext()) {
        var oListItem = listItemEnumerator.get_current();

        var itemUid = oListItem.get_item('ID');
        var colVal;
        var valSelect = " ";
        if (oListItem.get_item('Approval') != null) {
            valSelect = "selected";
            colVal = oListItem.get_item('Approval');
        } else {
            colVal = "N/A";
        }

        projItem = {};
        projItem.ID = oListItem.get_item('ID');
        projItem.priorityNumber = oListItem.get_item(levels);

        htmlTbl += "<tr class='NRM_pj_align'><td>" + itemUid + "</td>" +
        "<td>" + checkForNull(oListItem.get_item('Title')) + "</td>" +
        "<td>" + checkForNull(oListItem.get_item('Prioritization_x0020_Total')) + "</td>" +
        "<td data-order=" + oListItem.get_item('Priority') + "> " +
        "<input type='text' size='1' disabled name = 'rowID-" + oListItem.get_item('ID') + "' id='facilityRowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('Priority') + " value=" + checkForNull(oListItem.get_item('Priority')) + ">" +
        "</td>" +
        "<td data-order=" + oListItem.get_item('Priority_level2') + "> " +
        "<input type='text' size='2' disabled name = 'rowID-" + oListItem.get_item('ID') + "' id='STATERowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('Priority_level2') + " value=" + checkForNull(oListItem.get_item('Priority_level2')) + ">" +
        "</td>" +

        "<td data-order=" + oListItem.get_item('Priority_level3') + "> " +
        "<input type='text' size='2' disabled name = 'rowID-" + oListItem.get_item('ID') + "' id='nationalRowID-" + oListItem.get_item('ID') + "' data-curVal=" + oListItem.get_item('Priority_level3') + " value=" + checkForNull(oListItem.get_item('Priority_level3')) + ">" +
        "</td>" +

        "<td>" + checkForNull(oListItem.get_item('Project_x0020_Number')) + "</td>" +
        "<td>" + checkForNull(oListItem.get_item('STATE')) + "</td>" +
        "<td>" + checkForNull(oListItem.get_item('Station')) + "</td>" +

        "<td>" + checkForNull(oListItem.get_item('Total_x0020_Project_x0020_Cost')) + "</td>" +
        "<td>" + constructionDesign(oListItem.get_item('Design_x0020_Obligation_x0020_Da'), oListItem.get_item('Services'), "Services") + "</td>" +
        "<td>" + constructionDesign(oListItem.get_item('Construction_x0020_Obligation_x0'), oListItem.get_item('Construction_x0020_Cost'), "Budget") + "</td>" +      
        "<td><select id='Approval' class='oApproval" + itemUid + "' onchange='update(\"NF Operating Plan\"," + itemUid + ",\"Approval\")'><option value='' selected></option><option value='NF Operating Plan'" + valSelect + ">NF Operating Plan</option></select></td></tr>";

        projItemArray[i] = projItem;
        i++;
    }
}

Open in new window


Here's an image of what it looks like when I capture what's going on in the developer tool.
User generated image
I also created a mock-up in jsfiddle.
https://jsfiddle.net/isogunro/fkspbzh9/4/
Avatar of Leonidas Dosas
Leonidas Dosas
Flag of Greece image

Check this.I use sort method like the following code:
var projItemArray = new Array();

projItem = {};
projItem.ID = 2;
projItem.priorityNumber = 12;

projItem = {};
projItem.ID = 4;
projItem.priorityNumber = 14;

projItemArray[3] = projItem;

projItem = {};
projItem.ID = 5;
projItem.priorityNumber = 15;

projItemArray[4] = projItem;
projItemArray[1] = projItem;

projItem = {};
projItem.ID = 3;
projItem.priorityNumber = 13;

projItemArray[2] = projItem;
projItem = {};
projItem.ID = 1;
projItem.priorityNumber = 11;

projItemArray[0] = projItem;


projItemArray.sort(function(a, b){
    return a.priorityNumber-b.priorityNumber;
});
console.log(projItemArray);

Open in new window

Avatar of Isaac

ASKER

HI,

That does not render in the console correctly.  See below.
User generated image
Avatar of Isaac

ASKER

As you can see above, it modified my ID's
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Isaac

ASKER

That did not work either.  see fiddle
https://jsfiddle.net/isogunro/fkspbzh9/5/
Avatar of Isaac

ASKER

Never mind.  It did work. sorry