Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to arange columns

    I want to arrage the arr in the same as the cols. I uploaded screan shots of the vars. My code seems to not be working

    arr...
    Trying to arange columns-arr.png

    cols
    Trying to arange columns-cols.png
    Code:
    select: function (arr, cols) {
    
    			var x, j,
    				r = [],
    				i = arr.length - 1;
    			for (i; i >= 0; i -= 1) {
    				x = {};
    				for (j = 0; j < cols.length; j += 1) {
    					x[cols[j]] = arr[i][cols[j]];
    				}
    				r.push(x);
    			}
    			return r;
    		},

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,106
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Question


    Can you provide a short example of your array to be sorted?
    Arrange as fixed length rows in simple array or row-column 2-dimension array?
    What is output supposed to look like from your example array?

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,576
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    I am *guessing* that his cols variable (parameter to the function) looks like this:
    Code:
    var cols = ["id","systemid","funcareaid","siteid","recordtypeid","jcodeid","locationid","infoonly"];
    And he wants the data from the arr argument returned, still an array, ordered the same as cols.

    But this is the part I don't understand:arr[i][cols[j]]

    I can't make any sense of that, no matter what the arr looks like.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,576
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    I *think* this does the job:
    Code:
    function reOrder( inarr, cols )
    {
        var arr = [ ];
        for ( var a = 0; a < inarr.length; ++a )
        {
            var pair = inarr[a];
            arr[pair[0]] = pair[1];
        }
        var result = [ ];
        for ( var c = 0; c < cols.length; ++c )
        {
            result[cols[c]] = arr[cols[c]];
        }
        return result;
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,106
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Arrow

    Same conclusion.
    That's why I was asking the questions I posed.

  • #6
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ya. I don't know javascript to well to show you the array, but the above code does not work

  • #7
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am passing this
    ty = E.Util.reOrder(ty, ['id', 'systemid', 'funcareaid', 'siteid', 'recordtypeid', 'jcodeid', 'locationid', 'infoonly']);

  • #8
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't know if this helpsTrying to arange columns-1.png

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,576
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Sorry! This line:
    Code:
            result[cols[c]] = arr[cols[c]];
    should have been this, instead:
    Code:
            result[c] = [ cols[c], arr[cols[c]] ];
    The first code *does* return an ordered array, but not one you would have to use for ( var e in arr ) to traverse, not for ( i = 0, ...).

    Anyway the fix works probably as you expect it. Here's a simple demo:
    Code:
    <script>
    
    function reOrder( inarr, cols )
    {
        var arr = [ ];
        for ( var a = 0; a < inarr.length; ++a )
        {
            var pair = inarr[a];
            arr[pair[0]] = pair[1];
        }
        var result = [ ];
        for ( var c = 0; c < cols.length; ++c )
        {
            result[c] = [ cols[c], arr[cols[c]] ];
        }
        return result;
    }
    
    var data = [
        ["foo", 131],
        ["bar", "barred" ],
        ["id", 0 ],
        ["pc", "hmmm" ],
        ["apple", "good" ]
    ];
    
    var order = ["id","apple","bar","foo","pc"];
    
    document.write( data + "<hr>" );
    
    var orderedData = reOrder( data, order );
    
    document.write( orderedData + "<hr>" );
    </script>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    New to the CF scene
    Join Date
    Jan 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think the issue is that inarr is being passed as a dictionary. Your cade will arrage them in the order of "cols", but I loose the values. The first pic is what is going in. The second pic is your results

    Trying to arange columns-dic.pngTrying to arange columns-results.png

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,576
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    Ahhh...I thought inarr was an array of arrays, not an array of objects.

    Blame my old eyes.

    Try this. UNTESTED. Will test it later if it does not work.
    Code:
    function reOrder( arr, cols )
    {
        var result = [ ];
        for ( var c = 0; c < cols.length; ++c )
        {
            result[c] = arr[cols[c]];
        }
        return result;
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,576
    Thanks
    80
    Thanked 4,497 Times in 4,461 Posts
    No, I don't think that does work.

    Can you show the *CODE* that generates your starting array. Not a screen dump, which doesn't tell me enough.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,106
    Thanks
    38
    Thanked 498 Times in 492 Posts
    Quote Originally Posted by Old Pedant View Post
    No, I don't think that does work.

    Can you show the *CODE* that generates your starting array. Not a screen dump, which doesn't tell me enough.
    Hey, that's what I asked for way back in post #2!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •