Hello everyone,
I'm trying to sort a two-dimensional array by it's second[1] and third[2] position.
I managed to sort it by it's first[0] position with the simple sort() function, but I can't seem to find any working solution to what I want.
I've added my code and the final output I'm trying to achieve:
printArr() and sortByName() works great! I need help with the sortByPhone() and sortByEmail() ones.
Code:
<script type="text/javascript">
var studentArr = new Array();
studentArr[0] = new Array("Lior Iluz", "03-7262123", "lior@domain.com");
studentArr[1] = new Array("David Abutbul", "08-5232424", "david@domain.com");
studentArr[2] = new Array("Lior Bourla", "04-6232424", "lior@domain.com");
studentArr[3] = new Array("Adi Efrati", "08-9232427", "adi@domain.com");
studentArr[4] = new Array("Yossi Morad", "04-4232427", "yossi@domain.com");
studentArr[5] = new Array("Avishai Davis", "03-6232427", "avishai@domain.com");
studentArr[5] = new Array("Petra Russo", "04-7232427", "petra@domain.com");
function printArr(){
document.getElementById('table').innerHTML = '<h2>Students List</h2';
document.getElementById('table').innerHTML += '<p><span class="col1"><font color="#265e16"><strong>#</strong></font></span></span><span class="col2"><a href="index.html" title="Sort By Name" onclick="sortByName();return false;">Name</a></span><span class="col3"><a href="#" onlick="sortByPhone();return false;">Phone</a></span><span class="col4"><a href="#" onlick="sortByEmail();return false;">Email</a></span></p><div class="cleaner"></div>';
for (var i = 0; i < studentArr.length; i++) {
document.getElementById('table').innerHTML += '<p><span class="col1">' + i + '</span><span class="col2">' + studentArr[i][0] + '</span><span class="col3">' + studentArr[i][1] + '</span><span class="col4">' + studentArr[i][2] + '</span></p><div class="cleaner"></div>';
}
}
//a working simple sort - sorts by the first[0] position
function sortByName(){
studentArr.sort();
printArr();
}
//a tryout with a script I've found while searching all over google ;)
function numberSort(a,b){
return ((a[1] < b[1]) ? -1 : ((a[1] > b[1]) ? 1 : 0));
}
function sortByPhone(){
studentArr.sort(numberSort());
printArr();
}
//same as sortByPhone() but I thought maybe it'll work on this one :\
function sortByEmail(){
studentArr.sort(numberSort());
printArr();
}
</script>
That calls the function numberSort and then srots the array using the function returned, but no function is returned. Instead you want
Code:
studentArr.sort(numberSort);
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
Charles, I deleted the spare () and now something happens but not the right result... it just sorts it out with no recognized order... something wrong with the numberSort() function?
Other than that I'm not sure just what you are doing wrong, but here's a woking example:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Example</title>
</head>
<body>
<ul>
<script type="text/javascript">
<!--
var studentArr = new Array();
studentArr[0] = new Array("Lior Iluz", "03-7262123", "lior@domain.com");
studentArr[1] = new Array("David Abutbul", "08-5232424", "david@domain.com");
studentArr[2] = new Array("Lior Bourla", "04-6232424", "lior@domain.com");
studentArr[3] = new Array("Adi Efrati", "08-9232427", "adi@domain.com");
studentArr[4] = new Array("Yossi Morad", "04-4232427", "yossi@domain.com");
studentArr[5] = new Array("Avishai Davis", "03-6232427", "avishai@domain.com");
studentArr[5] = new Array("Petra Russo", "04-7232427", "petra@domain.com");
function byName (a, b) {return a[0] == b[0] ? 0 : a[0] < b[0] ? -1 : 1}
function byNumber (a, b) {return a[1] == b[1] ? 0 : a[1] < b[1] ? -1 : 1}
function byAddress (a, b) {return a[2] == b[2] ? 0 : a[2] < b[2] ? -1 : 1}
studentArr.sort(byAddress);
for (i = 0; i < studentArr.length; i++) document.write ('<li>', studentArr[i], '<\/li>');
// -->
</script>
</ul>
</body>
</html>
“The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect.”
—Tim Berners-Lee, W3C Director and inventor of the World Wide Web
Weird thing... if I use your code alone it works like a charm... but if I only use your functions and call them to fit my code - it doesn't work good (changes the order but not in the right way).
I'll check what causes that in my code...
Bookmarks