4 Replies - 1234 Views - Last Post: 08 October 2011 - 11:02 AM

#1 mwstone56  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 12
  • Joined: 21-April 11

First and last name with last name alphabetized in javascript.

Posted 07 October 2011 - 06:25 PM

I have tried to find the answer. I have been asked to document.write my array by first name, then last name. However, I am being asked to alphabetize the list by the last name. I am pretty sure this involves a substring. I have been all over the internet trying to learn a method to do this. Yes, this is for a class but I am just asking that you point me in the right direction. Thanks


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

<script type="text/javascript">
   var namesList = new Array();

   function addName() {
      var firstName = document.nameForm.fName.value;
      var lastName = document.nameForm.lName.value;

      namesList[namesList.length] = firstName + ' ' + lastName;

      document.nameForm.reset();
   }

   function listNames() {
      document.write('Names added to the list:');
      document.write('<br/><br/>');

      sortNamesByFirst();

      for (i = 0; i < namesList.length; i++) {
         document.write(namesList[i]);
         document.write('<br/>');
      }  
   }

   function sortNamesByFirst() {
      for (i1 = 0; i1 < namesList.length; i1++) {
         
         for (i2 = 0; i2 < namesList.length - 1; i2++) {
     
            var name1 = namesList[i2];
            var name2 = namesList[i2 + 1];
 
            var compareName1 = name1.substring(0, name1.indexOf(' '));
            var compareName2 = name2.substring(0, name2.indexOf(' '));

            if (compareName1 > compareName2) {
               namesList[i2] = name2;
               namesList[i2 + 1] = name1;
            }
         }
      }
   }

</script>

</head>

<body>
   <form action="" name="nameForm">
      <fieldset>
         First name: <input type="text" name="fName" /><br/>
         Last name: <input type="text" name="lName" /><br/>
      </fieldset>

      <br/>
      <input type="button" value="Add Name to List" onclick="addName()">

   </form>

   <br/>
   <button type="button" onclick="listNames()">List Names</button>
</body>

</html>





Is This A Good Question/Topic? 0
  • +

Replies To: First and last name with last name alphabetized in javascript.

#2 JMRKER  Icon User is offline

  • D.I.C Addict

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

Re: First and last name with last name alphabetized in javascript.

Posted 07 October 2011 - 06:52 PM

Why not create two arrays?
One would be with Fname Lname [say firstNameList]
and the other with Lname, Fname? [say lastNameList]
Created the same way you currently make "namesList" array,
just store in the display order (first last or last, first) desired.

Then you can sort either (or both) with
firstNameList.sort() and lastNameList.sort()

The substring logic in the sort function would not be needed.

I'm not sure I understand the requirement to use document.write()
as you will not be able to add any additional names after
displaying the lists one time.
Was This Post Helpful? 1
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: First and last name with last name alphabetized in javascript.

Posted 08 October 2011 - 03:25 AM

View PostJMRKER, on 07 October 2011 - 09:52 PM, said:

Why not create two arrays?


No, no, NO! Parallel arrays are bad. Almost always. Please don't advocate the use of the evil things.

Right, you're making an array of namesList[namesList.length] = firstName + ' ' + lastName;. By combining them like that, you're forcing yourself to uncombine them later. Why not just store both values an combine them later?

You could make a Person object, with a proper constructor. However, for a quick and dirty:
function addName() {
	var firstName = document.nameForm.fName.value;
	var lastName = document.nameForm.lName.value;

	namesList[namesList.length] = { 'first':firstName, 'last':lastName };

	document.nameForm.reset();
}

function listNames() {
	document.write('Names added to the list:');
	document.write('<br/><br/>');

	sortNamesByFirst();

	for (i = 0; i < namesList.length; i++) {
		document.write(namesList[i].first + ' ' + namesList[i].last);
		document.write('<br/>');
	}  
}



I'll leave the sorting to you.
Was This Post Helpful? 2
  • +
  • -

#4 JMRKER  Icon User is offline

  • D.I.C Addict

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

Re: First and last name with last name alphabetized in javascript.

Posted 08 October 2011 - 08:03 AM

View Postbaavgai, on 08 October 2011 - 04:25 AM, said:

View PostJMRKER, on 07 October 2011 - 09:52 PM, said:

Why not create two arrays?


No, no, NO! Parallel arrays are bad. Almost always. Please don't advocate the use of the evil things.

...
I'll leave the sorting to you.


Normally I would agree with you that parallel array are not the best way to go.

But,

This looks like a very simple homework question and the easiest way for this (I suppose) beginner would be to create two separate, but self-contained, arrays that become the only things to manipulate.
Then there is no splitting, joining, substring or other coding tricks to create the very simple .sort() array display.

This post has been edited by JMRKER: 08 October 2011 - 08:04 AM

Was This Post Helpful? 1
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5801
  • View blog
  • Posts: 12,638
  • Joined: 16-October 07

Re: First and last name with last name alphabetized in javascript.

Posted 08 October 2011 - 11:02 AM

View PostJMRKER, on 08 October 2011 - 11:03 AM, said:

This looks like a very simple homework question and the easiest way for this (I suppose) beginner would be to create two separate, but self-contained, arrays that become the only things to manipulate.


I understand your point. However, sorting parallel arrays is usually the thing that teaches people not to use them. In javascript, an object and object declaration is beginner level. If it was more advanced, I would have introduced a object creation function and a prototype for toString.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1