I want to sort four dimensional array based on 0th index by using following method,

Here is the code:

var main_arr = [
    []
];
var hdnFromValues = [11,16,12,17,14,18,15];
var hdnToValues = [12,17,13,18,15,19,16];
var hdnSPIDs = [11,12,13,14,0,0,0];
var hdnFlag = [D,E,E,D,A,A,A];
for (var j = 0; j < hdnFromValues.length; j++) {
    var temp_arr = [];
    var HdnToValue;
    temp_arr.push(hdnFromValues[j]);
    temp_arr.push(hdnToValues[j]);
    temp_arr.push(hdnSPIDs[j]);
    temp_arr.push(hdnFlag[j]);
    main_arr.push(temp_arr);
    temp_arr = null;
}
main_arr.sort(sort_by_col);
hdnFrom = "";
hdnTo = "";
spid = "";
flags = "";
for (var i = 1; i < main_arr.length; i++) {
    hdnFrom = hdnFrom.concat(main_arr[i][0], ",");
    hdnTo = hdnTo.concat(main_arr[i][1], ",");
    spid = spid.concat(main_arr[i][2], ",");
    flags = flags.concat(main_arr[i][3], ",");
}
hdnFrom = hdnFrom.substring(0, hdnFrom.length - 1);
hdnTo = hdnTo.substring(0, hdnTo.length - 1);
spid = spid.substring(0, spid.length - 1);
flags = flags.substring(0, flags.length - 1);
alert(hdnFrom);
alert(hdnTo);
alert(spid);
alert(flags);

function sort_by_col(a, b) {
    return a[0] - b[0];
}

Sometimes, 2 & 3th index not arranging correctly as per 0th index.

Will someone help me?

what is the best way for four dimensional sorting? Want to sort based on 0th Index, all other indexes should moves along with 0th index. OR My sorting method is correct?

JSfiddle

I honestly didn't understand what you are trying to do...

Let's walk through it...

// You have four initial arrays
var A = [11,16,12,17,14,18,15];
var B = [12,17,13,18,15,19,16];
var C = [11,12,13,14,0,0,0];
var D = [D,E,E,D,A,A,A];

// Then you merge them into one (the merged would be like this)
var temp = [
        [11, 12, 11, D],
        [16, 17, 12, E],
        [12, 13, 13, E],
        [17, 18, 14, D],
        [14, 15, 0, A],
        [18, 19, 0, A],
        [15, 16, 0, A]
    ];

// And then you sort it...
temp.sort(sort);

// What's the result you are expecting? 
var result = ?????

If you can explain the result you are aiming, maybe I can help you do math.

// You have four initial arrays
var A = [0,16,12,0,14,18,15];
var B = [0,17,13,0,15,19,16];
var C = [11,12,13,14,0,0,0];
var D = [D,E,E,D,A,A,A];
// Then you merge them into one (the merged would be like this)
var temp = [
        [0, 0, 11, D],
        [16, 17, 12, E],
        [12, 13, 13, E],
        [0, 0, 14, D],
        [14, 15, 0, A],
        [18, 19, 0, A],
        [15, 16, 0, A]
    ];
// And then you sort it...
temp.sort(sort);
// What's the result you are expecting? 
var result = ?????

var result = var temp = [
        [0, 0, 11, D],
        [0, 0, 14, D],
        [12, 13, 14, E],
        [14, 15, 0, A],
        [15, 16, 0, A],
        [16, 17, 12, E].
        [18, 19, 0, A]

    ];

This is the result i expecting. (Note: i changed you values little)

Much clear now, here you go...

My original attempt

var A = [0,16,12,0,14,18,15],
    B = [0,17,13,0,15,19,16],
    C = [11,12,13,14,0,0,0],
    D = ['D','E','E','D','A','A','A'];

var X = function(vectors) {
            var arr = [];

                for ( var r=0,rl=vectors[0].length;r<rl;r++ ) {
                    arr.push([]);
                    for( var i=0,il=vectors.length;i<il;i++ ) {
                        arr[r][i] = vectors[i][r];
                    }
                }
                arr.sort(function(a, b) {
                    var r=0;
                    for(var i=0,il=a.length;i<il;i++){
                        r = a[i] - b[i];
                        if ( r != 0 ) 
                            break;
                    }
                    return r;
                });             
            return arr;
        }([A,B,C,D]);

And the function:

function mergeAndSortVectors(vectors) {
    var arr = [];

    // Merge
    for ( var r=0,rl=vectors[0].length;r<rl;r++ ) {
        arr.push([]);
        for( var i=0,il=vectors.length;i<il;i++ ) {
        arr[r][i] = vectors[i][r];
        }
    }

    // Sort
    arr.sort(function(a, b) {
        var r=0;
            for(var i=0,il=a.length;i<il;i++){
            r = a[i] - b[i];
            if ( r != 0 ) 
                break;
            }
            return r;
        });             
    return arr;
}

This is actually quite simple if approached the right way.

First, let's define the problem in more standard terms :

  1. take a collection of n congruant arrays each containing m elements
  2. transform the collection to an array of m arrays each containing n elements (effectively a matrix row-column transposition)
  3. perform a muti-criterion sort on the transformed data.

Now, all you need is a couple of efficient utilities to perform steps 2 and 3.

For step 2 :

//By Airshow
//Note: this function would benefit from some safety
//particularly input data checks.
function transform() {
    var args = [].slice.call(arguments);
    return args[0].map(function(val, i) {
        return args.map(function(aa, j) {
            return aa[i];
        });
    });
}

For step 3 :

/*
 * Copyright 2013 Teun Duynstee Licensed under the Apache License, Version 2.0 
 * https://raw.githubusercontent.com/Teun/thenBy.js/master/thenBy.min.js
 */
firstBy=(function(){function e(f){f.thenBy=t;return f}function t(y,x){x=this;return e(function(a,b){return x(a,b)||y(a,b)})}return e})();

Here's the data :

var hdnFromValues = [0,16,12,0,14,18,15];
var hdnToValues = [0,17,13,0,15,19,16];
var hdnSPIDs = [11,12,13,14,0,0,0];
var hdnFlag = ['D','E','E','D','A','A','A'];

And here's how to make the utilities operate on the data :

var result = transform(hdnFromValues, hdnToValues, hdnSPIDs, hdnFlag).sort(
    firstBy(function(a, b) { return a[0] - b[0]; })//numeric
    .thenBy(function(a, b) { return a[1] - b[1]; })//numeric
    .thenBy(function(a, b) { return a[2] - b[2]; })//numeric
    .thenBy(function(a, b) { return (a[3] < b[3]) ? -1 : (a[3] > b[3]) ? 1 : 0; })//alphabetic
);

DEMO

Note: you are definitely sorting a four dimensional array here - primarily because there is no four dimensional array to sort!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.