Results 1 to 3 of 3
  1. #1
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Yesterday I was viewing a chart of scores. Each column represented one judge, and each row, one contestant. I wanted to simplify the data by converting the scores to ranks, that is, for each judge, replace the highest score with 1, the next highest with 2, and so forth. The table happened to be in HTML, so I would be coding this in JavaScript, but what I need is a general idea of how best to approach the problem. Any suggestions (besides using Excel)?

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    I'm sure there are more efficient ways, but a rather clunky way is to use a double loop for each column in which the inner loop counts the number of entries that is at least the current entry in the outer loop.

    Pseudocode:

    Code:
    for (i=1;i<=n;i++)
    { 
      rank[i]=0;
      for (j=1;j<=n;j++)
      {
    	if (element[j]>=element[i])
    	{
    	  rank[i]=rank[i]+1;
    	}
      }
    }

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Thanks, Hans. I ended up "cheating" -- I devised a way to store the row reference along with the score, by creating a decimal separated pair of values I then could sort in descending numeric order.

    Code:
    function rankIt() {
      var tbl, cmax, rmax, i, j, k, points, r, s, t, v;
      // Create object reference to results table
      tbl = document.getElementsByTagName("table")[0];
      // Compute highest column index to play with, and highest row index
      cmax = tbl.rows[0].cells.length - 2; // ignore two right columns
      rmax = tbl.rows.length;
      for (i=2; i<cmax; i++) { // Skip first two columns
    	// Create blank array for this judge
    	points = new Array();
    	for (j=1; j<rmax; j++) { // Skip first row
    	  // Mutiply score by 100 and truncate
    	  v = parseInt(parseFloat(tbl.rows[j].cells[i].firstChild.firstChild.nodeValue) * 100);
    	  // Concatenate score, a . and the row index and add to 0-based array
    	  points[j-1] = v.toString() + "." + j.toString();
    	}
    	// Sort in numeric descending order
    	points.sort(desc);
    	for (k=0; k<points.length; k++) {
    	  // Create an empty span to hold the rank
    	  s = document.createElement("span");
    	  // Compute the rank from the index of the sorted array, 
    	  // adjust for a tie with the previous item (doesn't handle n-way ties with n>2)
    	  if (k<1) {
    		t = document.createTextNode(k+1);
    	  } else {
    		if (points[k].split(".")[0] == points[k-1].split(".")[0]) {
    		  t = document.createTextNode(k);
    		}
    		else {
    		  t = document.createTextNode(k+1);
    		}
    	  }
    	  // Retrieve the row number
    	  r = points[k].split(".")[1];
    	  // Insert a line break element
    	  tbl.rows[r].cells[i].firstChild.insertBefore(document.createElement("br"), tbl.rows[r].cells[i].firstChild.firstChild);
    	  // Append rank to blank span
    	  s.appendChild(t);
    	  // Insert span containing rank
    	  tbl.rows[r].cells[i].firstChild.insertBefore(s, tbl.rows[r].cells[i].firstChild.firstChild);
    	}  
      }
    }
    function desc(a, b ) {return b-a;}

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •