473,395 Members | 1,639 Online

# sort multiarray

if I have one simple array
1
4
2
6

for to order the code is:
arrayOrdin=arraySimple.sort();
and I have
1
2
4
6
but if array isn't simple:
1,car,blue
4,book,red
2,house,green
6,bicycle,pink

how I can to make the order based only on first column?;
fo to have this

1,car,blue
2,house,green
4,book,red
6,bicycle,pink
May 26 '07 #1
4 2004
On 26 Mai, 12:05, artev <mailnotspa...@notspamm.nnwrote:
if I have one simple array
1
4
2
6

for to order the code is:
arrayOrdin=arraySimple.sort();
and I have
1
2
4
6

but if array isn't simple:
1,car,blue
4,book,red
2,house,green
6,bicycle,pink

how I can to make the order based only on first column?;
fo to have this

1,car,blue
2,house,green
4,book,red
6,bicycle,pink
You can call an userdefined function to do the sorting of
multidimentional arrays.
Example:
[snip]
function sortByNumber(a, b){
return a[0] - b[0];
}

function sortByType(a, b){
var a2 = a[1].toLowerCase();
var b2 = b[1].toLowerCase();
return ((a < b) ? -1 : ((a b) ? 1 : 0));
}

function sortByColor(a, b){
var a2 = a[2].toLowerCase();
var b2 = b[2].toLowerCase();
return ((a < b) ? -1 : ((a b) ? 1 : 0));
}

arraySortNum = arraySimple.sort(sortByNumber);
arraySortType = arraySimple.sort(sortByType);
arraySortColor = arraySimple.sort(sortByColor);
[/snap]

purcaholic

May 26 '07 #2
why in the simple sort the final order is different respect at
sort of an array?
I use the same role for a list simpleand for a list that is the first
column of an array?
I want for the 1st column of array sort (a-b multiple in the table) how
a-b simpleM where is the error?

here the complete test:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<title>Doc</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script type="text/javascript">
//
//
function test3()
{
var cont_3a0=document.formbas.cont_3a0 ; var
cont_3a1=document.formbas.cont_3a1 ; var cont_3a2=document.formbas.cont_3a2
;

var cont_3b0=document.formbas.cont_3b0 ; var
cont_3b1=document.formbas.cont_3b1 ; var cont_3b2=document.formbas.cont_3b2
;
var cont_3bb0=document.formbas.cont_3bb0 ; var
cont_3bb1=document.formbas.cont_3bb1 ; var
cont_3bb2=document.formbas.cont_3bb2 ;
var cont_3bbb0=document.formbas.cont_3bbb0 ; var
cont_3bbb1=document.formbas.cont_3bbb1 ; var
cont_3bbb2=document.formbas.cont_3bbb2 ;
var cont_3bbbb0=document.formbas.cont_3bbbb0 ; var
cont_3bbbb1=document.formbas.cont_3bbbb1 ; var
cont_3bbbb2=document.formbas.cont_3bbbb2 ;
//var myarray_base0= new
Array(10,16,35,"0,1",8,4,22,19,1,4,-4,-8,-2,22,35,9,26,38,40);
// var myarray_base0= new
Array(10,8,1,"_casa","__bb",'002','003','004',005, 006,007,".thumb","._thumb",'auto','-parc0','bici','0_auto','BICI','CASA','casa');

var myarray_base0= new
Array(10,8,1,"_house","__bb",'002','003','004',005 ,006,007,".test","._test",'autoveicle','-park','bicycle','0_car','BICYCLE','HOUSE','house') ;

var myarray_base1= new
Array('a','b','c','d','e','f','g','h','i','l','m', 'n','o','p','q','r','s','t','u','v');
var myarray_base2= new
Array('aa','bb','cc','dd','ee','ff','gg','hh','ii' ,'ll','mm','nn','oo','pp','qq','rr','ss','tt','uu' ,'vv'
);
//0
cont_3a0.options.length=0;
var x=0;
for (var i = 0; i<myarray_base0.length; i++)
{ cont_3a0.options[x++]=new Option( myarray_base0[i]); }
//1
cont_3a1.options.length=0;
var x=0;
for (var i = 0; i<myarray_base1.length; i++)
{ cont_3a1.options[x++]=new Option( myarray_base1[i]); }
//2
cont_3a2.options.length=0;
var x=0;
for (var i = 0; i<myarray_base2.length; i++)
{ cont_3a2.options[x++]=new Option( myarray_base2[i]); }
//
================================================== ================================================== ===============
// SORT NORMAL

var order01=new Array();
order01=myarray_base0.sort();

cont_3b0.options.length=0;
var x=0;
for (var i = 0; i<order01.length; i++)
{ cont_3b0.options[x++]=new Option( order01[i]); }

//sort a-b
function sort0(a,b)
{
a - b;
}

var order01bb=new Array();
order01bb=myarray_base0.sort(sort0);

cont_3bb0.options.length=0;
var x=0;
for (var i = 0; i<order01bb.length; i++)
{ cont_3bb0.options[x++]=new Option( order01bb[i]); }

//
================================================== ================================================== ===============
// SORT WITH MULTICOLUMN

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

var arrayTotale=new Array();
for (var i = 0; i <myarray_base0.length; i++)
{
arrayTotale[i]=new Array();
arrayTotale[i][0]=myarray_base0[i];
arrayTotale[i][1]=myarray_base1[i];
arrayTotale[i][2]=myarray_base2[i];
}

var order01bb=new Array();
order01bb=arrayTotale.sort(sort2);

cont_3bb0.options.length=0;
cont_3bb1.options.length=0;
cont_3bb2.options.length=0;
var x=0;
for (var i = 0; i<arrayTotale.length; i++)
{ cont_3bb0.options[x++]=new Option( order01bb[i][0]);
cont_3bb1.options[x++]=new Option( order01bb[i][1]);
cont_3bb2.options[x++]=new Option( order01bb[i][2]);
}
}

</script>
<BODY>
<form name="formbas" action="" method="post">

<!-- -->
<!-- -->
<table border="1">
<tr>
<td>List </td><td></td><td></td>

<td>a-b simple </td>
<td></td><td></td>

<td>a-b multipl </td>
<td></td><td></td>

</tr>

<tr>
<td><select name="cont_3a0" size="22" style="width:100px" ></select></td>
<td><select name="cont_3a1" size="22" style="width:50px" ></select></td>
<td><select name="cont_3a2" size="22" style="width:50px" ></select></td>
<td><select name="cont_3b0" size="22" style="width:100px" ></select></td>
<td><select name="cont_3b1" size="22" style="width:50px" ></select></td>
<td><select name="cont_3b2" size="22" style="width:50px" ></select></td>

<td><select name="cont_3bb0" size="22" style="width:100px" ></select></td>
<td><select name="cont_3bb1" size="22" style="width:50px" ></select></td>
<td><select name="cont_3bb2" size="22" style="width:50px" ></select></td>

</tr>
</table>

<br>
<input type="button" name="puls" value="TEST3" onClick="test3()"
/><br><br><br>

<br><br>

<!-- -->
<!-- -->

<br><br>

</form>

</BODY>
</html>
May 26 '07 #3
artev said:
>
why in the simple sort the final order is different respect at
sort of an array?
I use the same role for a list simpleand for a list that is the first
column of an array?
I want for the 1st column of array sort (a-b multiple in the table) how
a-b simpleM where is the error?
This function does nothing:

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

I'll bet you meant:

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

You also need to work more on choosing the correct English prepositions.
I imagine that's fairly difficult to learn.
--

May 26 '07 #4
function sortByColor(a, b){
var a2 = a[2].toLowerCase();
var b2 = b[2].toLowerCase();
return ((a < b) ? -1 : ((a b) ? 1 : 0));
}
you use var a2 and var b2 but after in the expression they aren't used

in my simple test if I use:
function sort2(a,b){
a=a.toLowerCase();
b=b.toLowerCase();
if (a<b) return -1;
if (a>b) return 1;
return 0;
}

I have with consol javascript an error:
toLOwerCAse isn't a function
May 26 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.