2 Replies - 3644 Views - Last Post: 02 May 2012 - 04:21 AM

#1 TheBearKing  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 11-February 11

Sorting an array by different sub elements

Posted 30 April 2012 - 02:24 PM

Hello all,

So I have a program I'm writing where I have little league teams, their wins, losses, and ties. I have been asked to write the program to allow users to choose which method of sorting they would prefer: Sort by Wins, Points, or Alphabetically. (The points are 2 for a win, 1 for a tie, and 0 for a loss).

I have all the calculations for the points totals, but I have no idea how to begin doing the sorting.

My array is as follows (Team name, wins, ties, losses):

var rawteamstats = new Array;
rawteamstats[0] = ["Sharks", 3, 1, 2];
rawteamstats[1] = ["Jets", 2, 4, 1];
rawteamstats[2] = ["AppleDumplings", 3, 3, 1];



I am not really sure how to go about sorting by specific sub elements in each array. When writing the code to display the page/ HTML table, I came up with this:

var len = rawteamstats.length;	
	
function showStats(){
	rawteamstats.sort();	
		document.write("<html><body><table cols='6' border='1px' cellpadding='7'><tr><th colspan='7'>Team Standings</th></tr>");
		document.write("<tr><th>Team</th><th>W</th><th>L</th><th>T</th><th>Points</th></tr>");
	
	for (var i = 0; i <= len; i++){
		document.write("<tr><td>" + rawteamstats[i][0] + "</td><td>" + rawteamstats[i][1] + "</td><td>" + rawteamstats[i][2] + "</td><td>" + rawteamstats[i][3] + "</td><td>" + (rawteamstats[i][1] * 2 + (rawteamstats[i][3])) + "</td></tr>");
	};
	document.write("</table></body></html>");
}



Any ideas or suggestions? I know that each sort is going to have its own function, but not exactly sure on how to get started with it.

Any help would be appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Sorting an array by different sub elements

#2 JMRKER  Icon User is offline

  • D.I.C Addict

Reputation: 128
  • View blog
  • Posts: 828
  • Joined: 25-October 08

Re: Sorting an array by different sub elements

Posted 01 May 2012 - 07:52 PM

One possible solution:
<!DOCTYPE html>
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">

var rawteamstats = [
    ["Sharks", 3, 1, 2],
    ["Jets", 2, 4, 1],
    ["AppleDumplings", 3, 3, 1]
];
var tarr = [];

function sortList(fld) {
  tarr = [];  var ptr = 0;
  for (var i=0; i<rawteamstats.length; i++) {
    if (fld != 4) { tarr.push(rawteamstats[i][fld]+':'+i); }
    else {
      pts = (rawteamstats[i][1]*2)+rawteamstats[i][3];
      tarr.push(pts.toString()+':'+i);
    }
  }
  switch (fld) {
    case 0 : tarr.sort(); break;
    case 1 : tarr.sort().reverse(); break;
    case 2 : tarr.sort().reverse(); break;
    case 3 : tarr.sort().reverse(); break;
    case 4 : tarr.sort().reverse(); break;
  }
  document.getElementById('tbl').innerHTML=showStats();
}
function showStats() {
  var str = '';  var ptr = 0;
  str += "<table cols='6' border='1px' cellpadding='7'><tr><th colspan='7'>Team Standings</th></tr>";
  str += "<tr><th>Team</th><th>W</th><th>L</th><th>T</th><th>Points</th></tr>";
  for (var i=0; i<tarr.length; i++) {
    ptr = tarr[i].split(':');  p = Number(ptr[1]);
    str += "<tr><td>" + rawteamstats[p][0] + "</td><th>" + rawteamstats[p][1] + "</th>";
    str += "<th>" + rawteamstats[p][2] + "</th><th>" + rawteamstats[p][3] + "</th>";
    str += "<th>" + (rawteamstats[p][1] * 2 + (rawteamstats[p][3])) + "</th></tr>";
  };
  str += "</table>";
  return str;
}

window.onload = function() { sortList(0); }

</script>
</head>
<body>
Sort by: 
<button onclick="sortList(0)">Team</button>
<button onclick="sortList(1)">Wins</button>
<button onclick="sortList(2)">Lost</button>
<button onclick="sortList(3)">Ties</button>
<button onclick="sortList(4)">Points</button>
<br>
<div id="tbl"></div>
</body>
</html>


:bananaman:
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5826
  • View blog
  • Posts: 12,678
  • Joined: 16-October 07

Re: Sorting an array by different sub elements

Posted 02 May 2012 - 04:21 AM

Well, if you want to use the array sort method, then you can just pass it a compare function.

Liberally ripping off JMRKER's code, here's an example.
<html>
<head>
	<title>Sort Test</title>
	<script type="text/javascript">
	var rawteamstats = [
		["Sharks", 3, 1, 2],
		["Jets", 2, 4, 1],
		["AppleDumplings", 3, 3, 1]
	];


	function showStats(fieldNum) {
		var html = '';
		rawteamstats.sort(function (a, b ) { return a[fieldNum] < b[fieldNum]; });
		for (var i in rawteamstats) {
			var item = rawteamstats[i];
			html += "<tr>";
			for (var j in item) { html += "<td>" + item[j] + "</td>"; }
			html += "</tr>";
		};
		document.getElementById('tbl').innerHTML = html;
	}

	window.onload = function() { showStats(0); }

	</script>
</head>
<body>
	<p>Sort by:</p>
	<button onclick="showStats(0)">Team</button>
	<button onclick="showStats(1)">Wins</button>
	<button onclick="showStats(2)">Lost</button>
	<button onclick="showStats(3)">Ties</button>
	<table border="1">
		<caption>Team Standings</caption>
		<thead>
			<tr><th>Team</th><th>Wins</th><th>Lost</th><th>Ties</th></tr>
		</thead>
		<tbody id="tbl">
		</tbody>
	</table>
</body>
</html>


This post has been edited by baavgai: 02 May 2012 - 04:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1