Sarath
Sarath

Reputation: 9156

Sort a three dimensional array in Javascript

Consider this Array

var LIST =[];

LIST['C']=[];
LIST['B']=[];

LIST['C']['cc']=[];
LIST['B']['bb']=[];  

LIST['C']['cc'].push('cc0');
LIST['C']['cc'].push('cc1');
LIST['C']['cc'].push('cc2');
LIST['B']['bb'].push('bb0');
LIST['B']['bb'].push('bb1');
LIST['B']['bb'].push('bb2');

I can loop through this array like

  for(var i in LIST){

      console.log(i)//C,B
      var level1=LIST[i];

      for(var j in level1){
        console.log(j)//cc,bb
        // etc...
      }

   }

Fine.. I have few basic questions.

1.How to sort the array in each level?

One level can be sort by .sort(fn) method . How can i pass to inner levels?

2.Why the indexOf method does not works to find the elements in first two levels?

If it's because of the a non string parameter .. how can i search an array items in array if the item is not string?

3.How for(var i in LIST) works ? I just need a basic understanding of indexing and looping through array ..

Thanks ..

Upvotes: 2

Views: 1285

Answers (3)

xdazz
xdazz

Reputation: 160883

LIST is NOT a three dimensional array in Javascript, it is just an array.

//declare an array which names LIST.
var LIST = [];

//set a property named 'C' of the LIST to be an array.
LIST['C']=[];
//set a property named 'B' of the LIST to be an array.
LIST['B']=[];

//set a property named 'cc' of the 'LIST.C'(which is an array object)
LIST['C']['cc']=[];
//set a property named 'bb' of the 'LIST.B'(which is an array object)
LIST['B']['bb']=[];  

The fact is you only need to let the last level to be an array, see my example code below.

function iterateOrderd(obj) {
    if (obj instanceof Array) {
        obj.sort();
        for (var j = 0, l=obj.length; j < l; j++) {
            console.log(obj[j]);
        }
    } else {
        var sortable = [];
        for (var i in obj) {
            if (obj.hasOwnProperty(i)) {
                sortable.push(i);
            }
        }
        sortable.sort();
        for (var j = 0, l=sortable.length; j < l; j++) {
            console.log(sortable[j]);
            iterateOrderd(obj[sortable[j]]);
        }
    }
}


var LIST = {};

LIST['C'] = {};
LIST['B'] = {};

LIST['C']['cc']=[];
LIST['B']['bb']=[];  

LIST['C']['cc'].push('cc0');
LIST['C']['cc'].push('cc1');
LIST['C']['cc'].push('cc2');
LIST['B']['bb'].push('bb0');
LIST['B']['bb'].push('bb1');
LIST['B']['bb'].push('bb2');

iterateOrderd(LIST);

Upvotes: 1

user123444555621
user123444555621

Reputation: 153154

You need to know that Array inherits from Object.

In JavaScript, any Object instance is an associative array(!), so acts like an Array in PHP. For example:

var o = {}; // or new Object();
o['foo'] = 'bar';
o[0] = 'baz';
for (i in o) { console.log(i, o[i]); }

Sorting an Object does not make much sense. indexOf would kinda work in theory, but is not implemented.

Arrays are ordered lists. Array instances have push(), length, indexOf(), sort() etc., but those only work for numerical indexes. But again, Array inherits from Object, so any array can also contain non-numerical index entries:

var a = []; // or new Array();
a[0] = 'foo'; // a.length is now 1
a.push('baz'); // a[1] === 'baz'
a.qux = 1; // will not affect a.length
a.sort(); // will not affect a.qux
for (i in a) { console.log(i, a[i]); }

I recommend playing around with arrays and objects, and you'll soon get the point.

Upvotes: 1

0xc0de
0xc0de

Reputation: 8297

What is your sorting criteria ? I mean how will you say array firstArray comes before secondArray? regarding the for (counter in myArray), counter will take values of an array element in every iteration.

for (counter in [0,1,5]), counter will have values 0, 1 and 5 in the 3 iterations.

In your case, i will have values LIST['B'] and LIST['C'] in the two iterations and j will have values LIST['B']['bb'], LIST['B']['cc'], LIST['C']['bb'] and LIST['C']['cc'].

Both i and j will be arrays.

Upvotes: 0

Related Questions