0

Update: I seem to have fixed it by simply adding

document.getElementById('fullName').focus(); 
  NameSalaries.sort();
  } 

in the main function. I can't believe it actually worked. But I assume it's because I 'zipped' the main arrays. Thanks to everyone for your help!

Okay, I'm trying a new method. This way, the employees are added dynamically with a new table cell each time. The issue I'm having remains, however. I need the results to sort themselves as they are submitted. Both the name, and the salaries.

<!DOCTYPE html> 
<html lang="en"> 
<head> 
<meta charset="UTF-8" /> 
<title> Table Entry </title> 
</head> 
<body> 
<input type="text" id="fullName" value="" placeholder="Firstname"> :Enter employee name <br> 
<input type="text" id="salary" value="" placeholder="Salary"> :Enter employee salary <br> 
<input type="button" value="click me add to array" onclick="addToArray()"> <p>&nbsp;</p> 

<table border="1" id="source" cellspacing="3" cellpadding="3"> 
 <thead> 
  <tr> <th>Employee Name</th> <th>Employee Salary</th> </tr> 
 </thead> 
 <tbody id="sourceBody"> 
  <tr><td>&nbsp;</td><td>&nbsp;</td></tr> 
 </tbody> 
</table> 

<script type="text/javascript"> 

var NameSalaries= []; 
function addToArray() { 
  var fn = document.getElementById('fullName').value; 
  var sly = document.getElementById('salary').value; 
  var both = [fn,sly]; 
  NameSalaries.push(both); 
  createBody(NameSalaries); 
  document.getElementById('fullName').focus(); 
  } 
function createBody(NS) { 
  var tarr = []; 
  for (var i=0; i<NS.length; i++) { 
    tarr.push('<tr><td>'+NS[i][0]+'</td><td>'+NS[i][1]+'</td></tr>');  
  } document.getElementById('sourceBody').innerHTML = tarr.join(''); 
} 

</script> 
</body> 
</html>  
4
  • You'll need to zip them and then sort. Commented Jul 9, 2015 at 23:25
  • @Bergi: I was about to say that he should combine array items, then sort... I never heard about the zip you're referring to. Are we talking about the same thing? Commented Jul 9, 2015 at 23:28
  • This is definitely what I'm trying to do. I tried that with concat, but it didn't work. Any help? Sorry, very new to js Commented Jul 10, 2015 at 0:17
  • @The_Black_Smurf: Probably yes Commented Jul 10, 2015 at 0:34

1 Answer 1

1

You can join a salary and a name using a single object. For example:

var joe = {
    name: "joe schmoe",
    salary: 1234
};

Using this technique, you could "zip" your two arrays into one:

var employees = [];
for(var i = 0; i < names.length && i < salaries.length; i++) {
    employees.push({ // push appends to the end of the array
        name: names[i],
        salary: salaries[i]
    });
}

And you can pass a function to array.sort that will be used to compare values. So if you want to sort the employees array based on the employee names, you can compare strings using < and > for alphabetizing:

var alphabatizedEmployees = employees.sort(function(employee1, employee2) {
    return employee1.name > employee2.name;
});

And you can sort based on salary exactly the same way, except using the salary key instead of the name key:

var employeesSalarySorted = employees.sort(function(employee1, employee2) {
    return employee1.salary > employee2.salary;
});

sort returns a new array, so the original employees array will not be sorted.

I hope this helps.

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

2 Comments

Could I use this in conjunction with the names and salaries being defined by the user, rather than by me? Since the names and salaries are added dynamically after the page loads
Yes you could. Instead of pushing separate values to separate arrays when the user adds a new entry, you could push a single object to an array of objects. It looks like you're doing something similar in the code in your post except you're using a 2d array, so if you wanted to change it to use objects, you would change var both = [fn,sly] to var both = {name: fn, salary: sly} and '<tr><td>'+NS[i][0]+'</td><td>'+NS[i][1]+'</td></tr>' to '<tr><td>'+NS[i].name+'</td><td>'+NS[i].salary+'</td></tr>'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.