Sort and Display Data Stored in Arrays

Question:
How do I sort columns with their corresponding rows of information and then add data? I would like to use the data in a Web page.

Answer:

If you are using client-side JavaScript, you can store the data in arrays, then sort the arrays and regenerate an HTML table to display the newly sorted data. You will store each column’s worth of data in a separate array. Also, associate an “onclick” event with each column of the table and with a procedure that will execute and sort the array that is associated with that column (the one that was clicked).

Further, you must sort all other arrays in exactly the same order as the first so that all rows stay together. Use a simple sort routine, such as a bubble sort, to sort the first array, and include code that will move elements of all other arrays when an element of the first array is moved. The sort will look something like the following code. This code assumes that you have three arrays of data that will be displayed in columns of the table.

function bubbleSort(x, lim){	var i	var k	var temp		for(i=0; i x[k+1])			{				// Normally, we would use code like this, but since				// we are working with 3 arrays that must be 				// sorted together, use the next blocks of code				/*				temp=x[k]				x[k]=x[k+1]				x[k+1]=temp				*/								temp=Ages[k]				Ages[k]=Ages[k+1]				Ages[k+1]=temp								temp=Colors[k]				Colors[k]=Colors[k+1]				Colors[k+1]=temp								temp=Weights[k]				Weights[k]=Weights[k+1]				Weights[k+1]=temp			}		}	}}

Finally, you will completely re-generate the table. You will create a variable and concatenate an entire

tag and all the rows and columns (tags) of your table. If you are using IE, you can use a tag pair and set its innerHTML property of that to the value of your variable (holding the
tags and associated data). Remember to concatenate the newly sorted data from the arrays in the table. If you are using Netscape Navigator, you can do essentially the same thing with layers instead of a span.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS