3

The following is a table for the Google Charts API. I'm trying to sort the "Numbers" Column descending. Anyone know how to do this?

<script type="text/javascript" src="http://www.google.com/jsapi"></script> <script type="text/javascript"> google.load('visualization', '1', {packages: ['table']}); </script> <script type="text/javascript"> function drawVisualization() { // Create and populate the data table. var data = new google.visualization.DataTable(); data.addColumn('string', 'Names'); data.addColumn('number', 'Numbers'); data.addRows(3); data.setCell(0, 0, 'Name 1'); data.setCell(1, 0, 'Name 2'); data.setCell(2, 0, 'Name 3'); data.setCell(0, 1, 1); data.setCell(1, 1, 2); data.setCell(2, 1, 3); visualization = new google.visualization.Table(document.getElementById('table')); visualization.draw(data, null); } google.setOnLoadCallback(drawVisualization); </script> <div id="table"></div> 
1

1 Answer 1

6

Yes. just add the following line below your data def., it will sort descending on number, and then ascendsing on name.

data.sort([{column: 1, desc:true}, {column: 0}]); 

oh, you could also use this:

data.addRow(['Name 1',1]); data.addRow(['Name 2',2]); data.addRow(['Name 3',3]); 

regards, Halma

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.