1 Replies - 234 Views - Last Post: 22 January 2012 - 07:45 PM

Topic Sponsor:

#1 Tarsus  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 58
  • Joined: 06-September 10

Sorting Array

Posted 22 January 2012 - 12:30 PM

Hi,

Hope someone can help. I have created multiple arrays in JS as follows;

var tOne= new Array(0,"team one");
var tTwo= new Array(0,"team two");
var tThree= new Array(0,"team three");



I then have a function that gives the first element in the array a number ranking.
I want to create a table that ranks teams by the highest rank at the top.
To do this I want to rank by the first element in the array..

function sortNumber(a,B)
{
return a - b;
}

var n = [tOne[0],tOne[0],tOne[0]];
document.write(n.sort(sortNumber));



which seems to work.. however, I need a way to specify which team gets which rank. To give it an ID in effect.

Does anyone have any ideas?

This post has been edited by Tarsus: 22 January 2012 - 12:44 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Sorting Array

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 99
  • View blog
  • Posts: 653
  • Joined: 25-October 08

Re: Sorting Array

Posted 22 January 2012 - 07:45 PM

You don't show how the rankings will be changed
but you should be able to adapt this to your needs.
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
// From: 

var tOne= new Array(0,"team one");
var tTwo= new Array(0,"team two");
var tThree= new Array(0,"team three");

// for testing purposes, set ranking
tOne[0] = 3;  tTwo[0] = 2;  tThree[0] = 1;

// get current ranking
var teamInfo = [tOne,tTwo,tThree];
var ranking = [];
for (var i=0; i<teamInfo.length; i++) {
  ranking.push(teamInfo[i][0]+':'+teamInfo[i][1]);
}
ranking = ranking.sort();
alert(ranking.join('\n'));

</script>

<style type="text/css">

</style>
</head>
<body>

</body>
</html>


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1