Hello again.

I have an assignment that involves inputting the firstName, lastName and score of a student with the values being added to a textbox for display.

The problem is the last part of the assignment involves getting the last names sorted alphabetically which involves the use of an array.

My classmate has a solution but I feel it is way too complicated compared to the supposed "proper" way of doing it

Below is the Javascript code with a very lazy attempt at sorting the array. Any pointers would be appreciated.

var $ = function ()
		return document.getElementById(arguments[0]);
var avg = 0;
var count = 1;

var array = [];

window.onload = function()
		$("addScore").onclick = addScore_click;
		$("clear").onclick = clear_click;
		$("sort").onclick = sort_click;
var addScore_click = function()
		var lastName = $("lastname").value;
		var firstName = $("firstname").value;
		var score = parseInt ($("score").value);
		avg += score;
		if(score < 100)
			var res = lastName+ "," +firstName+ "," +score+"\n";
			$("t1").value += res;
			$("average").value = avg / count;
			count ++;
			array[array.length] += $("lastname").value,$("firstname").value,($("score").value);
			alert("Please enter a score less than 100");
var clear_click = function()
		$("t1").value = "";
		$("average").value  = "";

var sort_click = function()

Recommended Answers

All 4 Replies

Not sure what your question is here... Do you have to implement your own sort()? Also, why score must be lower than 100? Is the possible highest score is 99? If the possible highest score is 100, you need <= instead of <.

Thanks for pointing that out.

Anyway, I want any input passed in addScore_click to be stored in an array and I must be able to be able to sort them alphabetically by last name.

The problem is that you are trying to add 3 elements to the array at the same time, but they don't really get added. Have you ever tried to print it out? I think you would get something like this...

["undefinedlast", "undefinedlast", ...]

The structure of your array need to be changed. Now it would be how you want it to be. One way to do is to use and associated array with a regular array. What you need is to have one associated array using last name as its key, and values as an array of first name and score. The other array will keep all keys which are last name. When you sort it, you just use sort on the key array. When you display, you associate the key array with the associate array.

Everything Tay says plus ....

Array.sort() accepts a callback function as an argument, allowing you to define exactly how the sort is executed in terms of the sort criterion and sort direction.

First, store your results in an object as follows:

	lastName : lastName,
	firstName : firstName,
	score : score

Now write a function as follows:

var sortResults = function(columnName, dir) {
	dir = (Number(dir) < 0) ? -1 : (Number(dir) > 0) ? 1 : 0;//ensure dir is -1, 1 or 0.
	if( columnName && resultsArray[0][columnName] ) {
		var fn = function(A, B) {
			var a = (typeof resultsArray[0][column] === 'string') ? A[columnName].toLowerCase() : A[columnName];
			var b = (typeof resultsArray[0][column] === 'string') ? B[columnName].toLowerCase() : B[columnName];
			rtnVal = (a<b) ? -1 : (a>b) ? 1 : 0;
			return rtnVal * dir;

You now have the mechanism to sort on any of the three "columns", lastName|firstName|score, either ascending or descending, ie:

sortResults("lastName", 1); //sort ascending on lastName 
sortResults("lastName", -1); //sort descending on lastName
sortResults("firstName", 1); //sort ascending on firstName
sortResults("firstName", -1); //sort descending on firstName
sortResults("score", 1); //sort ascending on score
sortResults("score", -1); //sort descending on score

Your only real challenges then are to provide a good user interface to call sortResults in each of the six possible ways, and to print the sorted resultsArray to screen. If you could do that, then you should get a very good mark for the assignment.


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.