Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting array (again)

Status
Not open for further replies.

Peter74

Technical User
Jul 27, 2003
10
DK
Hello again,

I have an array defined like this:


function Library(x,y,z,p,q,r) {
this.x = x
this.y = y
this.z = z
this.p = p
this.q = q
this.r = r
}
library = new Array();
library[0] = ("...","...","...","...","...","...")


where "..." represents some information.

1) Can I use the javascript built-in sort() on one of the items in the array and how is it done? For example I tried library.x.sort(), but that doesn't work. Mozilla javascript console writes: library.x has no properties.

2) If I construct the array like this:
library = new Array();
without assigning a size to it then, when issuing:
library.length
it returns "6" (x,y.z.p.q.r) instead of the number of entries in the array. How do I make it return the number of entries in the array instead?

Thanks
/Peter
 
Peter,

try something more along the lines of:

Code:
<HTML>
<HEAD>
<SCRIPT>
function Library() {
    this.vars = new Array(6);
}

var lib = new Library();
alert(lib.vars.length); //6
lib.vars[0] = [&quot;third&quot;,&quot;second&quot;,&quot;first&quot;];
alert(lib.vars[0]); //returns third,second,first
alert(lib.vars[0].sort()); //returns first,second,third
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

Just having 6 variables in a function does not make the function an array of those variables.

I'm not sure if the code above answers your question, but have a look and post again.

Good luck.

--Dave
 
Hello Dave, thanks for your reply,

This is not exactly what I want. Your code will sort the entries in
Code:
lib.vars[i]
where &quot;i&quot; is some number. What I need to do is sort lib.vars based on one of the &quot;labels&quot; (I don't know the exect terminology here). For example in my example I might want to sort the entire library-array based on the label &quot;x&quot;. What I mean with that is that the sorting-function should look at all entries at position &quot;x&quot; in &quot;library&quot; and sort the library-array based on that.

I hope I'm not too confusing :) I don't know the terminology for this

/Peter
 
Okay, well, if you are certain to only have six variables (x,y,z,p,q,r), then you can (pardon me) hard code a correlation between the variable and the index in Library to which the label corresponds. Then, instead of calling sort(), you can call another function which reads the label and then calls the sort() function as such:

Code:
<HTML>
<HEAD>
<SCRIPT>
function Library() {
    this.vars = new Array(6);
}

function copyArray(array)
{
 var arrayCopy = new Array(array.length);
 for(i=0; i<array.length; i++)
 {
  arrayCopy[i] = array[i];
 }//end for

 return arrayCopy;
}//end function

function sortElement(lib,label)
{
 var sortedList;
 var arrayCopy;

 switch(label)
 {
  case 'x': arrayCopy = copyArray(lib.vars[0]);
            break;
  case 'y': arrayCopy = copyArray(lib.vars[1]);
            break;
  case 'z': arrayCopy = copyArray(lib.vars[2]);
            break;
  case 'p': arrayCopy = copyArray(lib.vars[3]);
            break;
  case 'q': arrayCopy = copyArray(lib.vars[4]);
            break;
  case 'r': arrayCopy = copyArray(lib.vars[5]);
            break;
  }//end switch

  sortedList = arrayCopy.sort();

  return sortedList;
}

var lib = new Library();
alert(lib.vars.length); //6
lib.vars[0] = [&quot;third&quot;,&quot;second&quot;,&quot;first&quot;];
alert(lib.vars[0]); //returns third,second,first
alert(sortElement(lib, &quot;x&quot;)); //returns first,second,third
alert(lib.vars[0]); //returns third,second,first
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>

Any better? [note, this does not permanently sort x... just a copy of x (remembering back to your previous sort question)].

--Dave
 
Hi Dave and thanks again :)

This is not exactly what I need. I haven't explained my problem well enough sorry. As I said in my previous posting; I don't know the terminology for all this.

Anyway, what you suggest will sort all entries in say
Code:
lib.vars[0]
alphabetically which is not what I want. I want to be able to sort all labeled elements (say x, y, z etc.) in the entire array
Code:
lib.vars[0], lib.vars[1],...,lib.vars[x]
alphabetically. For example, lets say lib.vars looks like this before sorting:
Code:
lib.vars[0] = (&quot;c&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;)
lib.vars[1] = (&quot;a&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;)
lib.vars[2] = (&quot;b&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;)
where &quot;a_info, b_info, c_info&quot; represents som information. Now, if I sort the array from the element &quot;x&quot; (first element in each entry) it should look like this:
Code:
lib.vars[0] = (&quot;a&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;)
lib.vars[1] = (&quot;b&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;)
lib.vars[2] = (&quot;c&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;)
The same goes if I choose to sort from y, z, etc.

If I can get this to work, it doesn't matter if the original array gets sorted as all related elements is represented en each of the six rows like this:
Code:
everything in lib.vars[0] is related, but unrelated with the information in lib.vars[1] etc.
I think I can make it work by extending your idea a little. I'll give it a try.

I'm trying to make this work as I think it's better than my previous six array model and it will probably make my searching-routine easyer to implement.

Do you have any suggestions for a better way of implementing such a text-based database?

Thanks
/peter
 
I THINK I have a handle on your question now:

if you have this:

Code:
lib.vars[0] = (&quot;c&quot;,&quot;d_info&quot;,&quot;h_info&quot;,&quot;j_info&quot;,&quot;n_info&quot;,&quot;r_info&quot;)
lib.vars[1] = (&quot;a&quot;,&quot;e_info&quot;,&quot;i_info&quot;,&quot;l_info&quot;,&quot;m_info&quot;,&quot;q_info&quot;)
lib.vars[2] = (&quot;b&quot;,&quot;f_info&quot;,&quot;g_info&quot;,&quot;k_info&quot;,&quot;o_info&quot;,&quot;p_info&quot;)

...and you sorted on 'x', then you want to end up with:

Code:
lib.vars[0] = (&quot;a&quot;,&quot;e_info&quot;,&quot;i_info&quot;,&quot;l_info&quot;,&quot;m_info&quot;,&quot;q_info&quot;)
lib.vars[1] = 
(&quot;b&quot;,&quot;f_info&quot;,&quot;g_info&quot;,&quot;k_info&quot;,&quot;o_info&quot;,&quot;p_info&quot;)
lib.vars[2] = (&quot;c&quot;,&quot;d_info&quot;,&quot;h_info&quot;,&quot;j_info&quot;,&quot;n_info&quot;,&quot;r_info&quot;)

...but if you sorted on 'y', then you want to end up with:

Code:
lib.vars[0] = (&quot;c&quot;,&quot;d_info&quot;,&quot;h_info&quot;,&quot;j_info&quot;,&quot;n_info&quot;,&quot;r_info&quot;)
lib.vars[1] = (&quot;a&quot;,&quot;e_info&quot;,&quot;i_info&quot;,&quot;l_info&quot;,&quot;m_info&quot;,&quot;q_info&quot;)
lib.vars[2] = (&quot;b&quot;,&quot;f_info&quot;,&quot;g_info&quot;,&quot;k_info&quot;,&quot;o_info&quot;,&quot;p_info&quot;)

...and if you sorted on, say, 'p', you want to end up with:

Code:
lib.vars[0] = (&quot;c&quot;,&quot;d_info&quot;,&quot;h_info&quot;,&quot;j_info&quot;,&quot;n_info&quot;,&quot;r_info&quot;)
lib.vars[1] = (&quot;b&quot;,&quot;f_info&quot;,&quot;g_info&quot;,&quot;k_info&quot;,&quot;o_info&quot;,&quot;p_info&quot;)
lib.vars[2] = (&quot;a&quot;,&quot;e_info&quot;,&quot;i_info&quot;,&quot;l_info&quot;,&quot;m_info&quot;,&quot;q_info&quot;)

Is this what you mean? If so, I think I can fathom a way to do what you want, but before I do, I want to verify I understand the question first.

--Dave
 
It sounds like you have an array of arrays and you want to sort on the first element of each of the sub-arrays. Is this correct? If so, this should do it:

<script>
function mySort(a,b){
if(a[0] > b[0]){return 1}
if(a[0] < b[0]){return -1}
return 0
}

lib=new Array();
lib[0] = [&quot;c&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;,&quot;c_info&quot;];
lib[1] = [&quot;a&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;,&quot;a_info&quot;];
lib[2] = [&quot;b&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;,&quot;b_info&quot;];

lib.sort(mySort);
</script>

Adam
 
An addition to my previous post... if you wanted to sort the inner arrays as well (leaving the label as the first element) you can use this:

<script>
function mySort(a,b){
if(a[0] > b[0]){return 1}
if(a[0] < b[0]){return -1}
return 0
}

lib=new Array();
lib[0] = [&quot;c&quot;,&quot;c_e&quot;,&quot;c_c&quot;,&quot;c_d&quot;,&quot;c_a&quot;,&quot;c_b&quot;];
lib[1] = [&quot;a&quot;,&quot;a_b&quot;,&quot;a_a&quot;,&quot;a_d&quot;,&quot;a_c&quot;,&quot;a_e&quot;];
lib[2] = [&quot;b&quot;,&quot;b_z&quot;,&quot;b_y&quot;,&quot;b_x&quot;,&quot;b_w&quot;,&quot;b_v&quot;];

lib.sort(mySort);

alert(lib);

for(var j=0;j<lib.length;j++){
lib[j]=[lib[j][0]].concat(lib[j].slice(1).sort());
}

alert(lib);
</script>

Adam
 
I'm terribly sorry, I should have given a better example. I'll try with a more real-world example:

Lets say I want to keep a
&quot;database&quot; of my CD's. I would do it like this:
Code:
function Library(band,album,year,genre) {
    this.band  = band
    this.album = album
    this.year  = year
    this.genre = genre
}

library = new Array();
    library[0] = (&quot;Metallica&quot;,&quot;And justice for all,&quot;1988&quot;,Metal&quot;)
    library[1] = (&quot;Sepultura&quot;,&quot;Arise&quot;,&quot;1991&quot;,&quot;Trash&quot;)
    library[2] = (&quot;Black Sabbath&quot;,&quot;Master of Reality&quot;,&quot;1976&quot;,&quot;Hard Rock&quot;)

Notice that everythin in library[0] is related, so is everything in library[1], library[2] etc. so I cannot just switch the library.band around if I sort the array by band. I'll have to switch everything in each entry in the array.

My original question was: Is it possible to sort this array using the javascript build-in
Code:
sort()
or maybe some other build-in sorting function? From the previous postings it looks as if that is not possible or is it? If it's not then I need some other way of sorting this array. Any suggestions?

Thanks
/Peter
 
I think I finally got it...

If you want to sort the libraries in alphabetical band order (for example), I believe you will have to create a new array of the bands, use the built-in sort() to sort THAT array, then mimic the order of the new array in re-ordering the library arrays. I have tested the following in IE with success:

Code:
<HTML>
<HEAD>
<SCRIPT>
function Library(band,album,year,genre) 
{
    this.arr = new Array(band,album,year,genre);
}

//LOAD LIBRARY:
var library = new Array();
library[0] = new Library(&quot;Metallica&quot;,&quot;And justice for 

all&quot;,&quot;1988&quot;,&quot;Metal&quot;);
library[1] = new Library(&quot;Sepultura&quot;,&quot;Arise&quot;,&quot;1991&quot;,&quot;Trash&quot;);
library[2] = new Library(&quot;Black Sabbath&quot;,&quot;Master of 

Reality&quot;,&quot;1976&quot;,&quot;Hard Rock&quot;);


function sortByColumn(col) 
//if col == 0: sort by band name
//if col == 1: sort by album
//if col == 2: sort by year (although this might be troublesome if the 

year is a String and this is not accounted for in the sort;
//if col == 3: sort by type of music
{
 var tempArray = new Array(library.length);
 for(i=0; i<library.length; i++)
 {
  //Add index to end of each entry and make upper case.  
  //E.g., tempArray[0] = &quot;METALLICA_0&quot;;
  //      tempArray[1] = &quot;SEPULTURA_1&quot;;
  //      etc.
  tempArray[i] = library[i].arr[col].toUpperCase() + &quot;_&quot; + i;
 }//end for

 tempArray.sort();
 var newArray = new Array(library.length);

 for(i=0; i<tempArray.length; i++)
 {
  //now that tempArray is ordered, strip down the
  // elements to just the index number they represent
  var current = tempArray[i];
  tempArray[i] = current.substring(current.lastIndexOf(&quot;_&quot;)+1);
  var libIndex = parseInt(tempArray[i]);
  newArray[i] = library[libIndex];
 }//end for

 //set library = to newArray and library is now sorted
 // by the desired column
 library = newArray;

 //To display sorted library if you want:
 var message = &quot;&quot;;
 for(i=0; i<library.length; i++)
  message += library[i].arr + &quot;\n&quot;;

 alert(message);
}//end function
</SCRIPT>
<body>
SORT BY:
<select name=&quot;num&quot;>
<option val=0>band name
<option val=1>album name
<option val=2>year
<option val=3>genre
</select>  
<input type=button value='sort' 

onclick=sortByColumn(num.selectedIndex)>
</BODY>
</HTML>

Is THIS what you're after?

--Dave
 
Here you go...

<script>
function Library(band,album,year,genre) {
this.band = band
this.album = album
this.year = year
this.genre = genre
}
function mySort(a,b){
if(a[sortOn] > b[sortOn]){return 1}
if(a[sortOn] < b[sortOn]){return -1}
return 0
}

var library = new Array();
library[0] = new Library(&quot;Metallica&quot;,&quot;And justice for all&quot;,&quot;1988&quot;,&quot;Metal&quot;)
library[1] = new Library(&quot;Sepultura&quot;,&quot;Arise&quot;,&quot;1991&quot;,&quot;Trash&quot;)
library[2] = new Library(&quot;Black Sabbath&quot;,&quot;Master of Reality&quot;,&quot;1976&quot;,&quot;Hard Rock&quot;)


var sortOn = &quot;band&quot; //change this to the column you want to sort on.
library.sort(mySort)
</script>

Adam
 
Thanks a lot to Dave & Adam who have patiently tried to help me with my sorting problem, I appreciate it :)

I don't have time to get into it right now, but I'll take a look at it tomorow evening. Hopefully it will work this time or I'll return with more questions :)

Thanks
/Peter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top