# Sorting Array hashes by a value in a 3rd dimension of the hash value

• Jul 31, 2002, 07:36
Flawless_koder
Sorting Array hashes by a value in a 3rd dimension of the hash value
I'm not trying to turn this into a little tutorial for me - but I'm just sharing some more of the stuff i'm working on right now.

You may remember the post recently on 3d Array hashes and handling of them in Javascript:

Now further along the road i realised i need to be able
to sort both up and down, and on a random column. (up and down == ascending / descending ).

I'll just post away and let you see what i've done:

Code:

``` // Random Number handling  var rF = new Object();  rF.current = new Date();  rF.urand  = rF.current.getTime();  function cR() {         rF.urand = (rF.urand*9301+49297) % 233280;         return rF.urand/(233280.0);         }  function vR(n) {         return Math.ceil(cR()*n);         }  // End  function valSort(A){         for (var f=0;f<(A.length-1);f++){                 for (var s=(f+1);s<A.length;s++){                         if (A[A[s]] < A[A[f]]){                                 var h = A[f]                                 A[f]  = A[s];                                 A[s]  = h;                                 }                         }                 }         }  if (wp3.sort_frame == 'car' && wp3.sort_column){         var cCol = ((wp3.sort_column.match(/Pkg /))?'tRate':'t'+wp3.sort_column);         var sortby = new Array();         for (i=0;i<sClass.length;){                 var tVendor  = sClass[i];                 var tmp_arr = eval(sClass[sClass[i]]);                 var tClass  = tmp_arr[0];                 var tRate  = eval(tmp_arr[1]);                 sortby[tVendor] = eval(cCol);                 sortby[sortby.length] = tVendor;                 sClass.splice(i,1);                 }         valSort(sortby);         if (wp3.sort_direction == 'sort_up') sortby.reverse();         for (i=0;i<sortby.length;i++){                 sClass.push(sortby[i]);                 }         }  else {         var random = new Array();         var steady = sClass.length;         for (i=0;i<steady;i++){                 var tmp_obj = sClass.splice((vR(sClass.length)-1),1);                 random.unshift(tmp_obj);                 }         for (i=0;i<random.length;i++){                 sClass.push(random[i]);                 }         if (sSupplier != '- ALL -'){                 for (i=0;i<sClass.length;i++){                         if (sClass[i] == sSupplier){                                 var tmp_obj = sClass.splice(i,1);                                 sClass.unshift(tmp_obj);                                 }                         }                 }         }```
Explainations:

The two functions are for the two respective sorting methods.

The first sort method occurs if it's a car result frame, and there's a sort_column set ( which happens by clicking on it ).
The second sort method happens automatically otherwise.

The second method puts the results of the hash ( a name, value with an array as the value ) in a random order.
It then stipulates that if a specific supplier is chosen - that one should be brought to the top - out of the random mess.

The first method workds by creating a new array of just
the name of the key, and a single value from the array value ( or the name itself )

I found that i needed to write the funciton myself because no sortFunction method ( see array.sort(sortFunction) ) could possibly compare the single element without loosing the integrity of the array.
I didn't simply pass sClass through the valSort because...
... actually - let me explain how i WOULD have - if i did ...
It would have worked by passing valSort an extra argument, stipulating the position of the element in the value array's scope to be looking at to sort.
The reason i DIDN'T do this is because i also wanted to be
able to sort by the name of the key on it's own.
I couldn't do this because it's outside the value array ( obviously - being the key for it ).
The solution for this might have been to have treated the name as a -1 element in the array - and name that before passing - although this could have thrown up unexpected results.

Well... that's my thoughts on the matter.

Anyone have any ideas on better methods?
Anyone have any questions, more importantly i guess.

Flawless