469,275 Members | 1,528 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

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 1902
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>
<head>
<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>
</head>
<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
Lee
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Kamilche | last post: by
4 posts views Thread by its me | last post: by
40 posts views Thread by Elijah Bailey | last post: by
7 posts views Thread by Stuart | last post: by
7 posts views Thread by DC Gringo | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
3 posts views Thread by artev | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.