473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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>
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
7
by: Stuart | last post by:
The stl::sort() that comes with Dev Studio 6 is broken (it hits the degenerate case in a common situation). I have a replacement. I would like to globally do "using namespace std; except use my...
7
by: DC Gringo | last post by:
I have a datagrid that won't sort. The event handler is firing and return label text, just not the sort. Here's my Sub Page_Load and Sub DataGrid1_SortCommand: -------------------- Private...
48
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a...
10
by: Woody Ling | last post by:
In 32 bits DB2 environment, is it meaningful to set sheapthres larger than 256MB for the following case.. 1. Intra-parallel is ON 2. Intra-parallel is OFF
3
by: artev | last post by:
in the default sort which is the compare function used? alphabetical ordination is so:arry.sort(); but which is the default function used? is there a good link wich example for alphabetical...
5
by: neehakale | last post by:
I know that heap sort,quick sort and merg sort are the faster sorting algoritms than the bubble sort,selection sort,shell sort and selection sort. I got an idea,in which situation we can use...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.