I'm using jQuery to parse a JSON file of employees, which contains their name, department, subdepartment, and some other details.
e.g.:
[
{
"LAST":"Betsy",
"FIRST":"Jackson",
"DEPT":"Customer Service",
"SUBDEPT":"Tech Support",
"JOINED":"02/94",
"TITLE":"Technical Customer Service Representative",
"RESIDENCE":"Someplace",
"HOBBIES":"Reading, Walking, Sleeping"
},
{
"LAST":"Sally",
"FIRST":"Smith",
"DEPT":"Customer Service",
"SUBDEPT":"Installation Customer Service Representative",
"JOINED":"01/04",
"TITLE":"Technical Customer Service Representative",
"RESIDENCE":"Someplace",
"HOBBIES":"Reading, Walking, Sleeping"
},
]
I'm trying to build an application where users can click on the name of an employee and see a refresh of results where every employee in that employee's department is shown, organized by the sub-departments, and scrolled-down to the given employee.
I've successfully generated a list of employee names, with data-* attributes to hold their department and sub-department. When an employee name is clicked, I've been able to parse the JSON file a second time return all the employees who are also in that department, and build a grid, and then push the entire matching employee object into a new array called "results."
note: dept = data-dept passed by jquery selector..
$.each(data, function(i, employee) {
if (employee.DEPT == dept) {
var employeetext = '<span class="name">'
+ employee.FIRST+' '+ employee.LAST+'</span>',
employee.JOINED,
employee.TITLE,
employee.RESIDENCE,
'...',
employee.HOBBIES;
$('#employees').append('<div class="employee_block"><img src="" width="85" height="113">' + employeetext + '.</div>');
results.push(employee);
}
}) // end stepping through employees
However, I need to build the grid based on a new sorted order from the array, rather than y the alphabetical that is being used now. I need to split the results by sub-department, according to a priority that is not alphabetical, but rather a custom order which I'm hoping to define in a separate object (would this be a "relational database?") e.g:
var subdeptorder = [
{
"DEPT": "Marketing",
"SUBDEPTS": ["Administration", "Purchasing", "Advertising", "PR"]
},
{
"DEPT": "Sales",
"SUBDEPTS": ["Administration", "Business Development"]
}
]
So I need to sort the "results" array according to the employee's department (and that department's subdepartment order) within it.
How do I write a comparison function to re-sort the "results" array according to a priority established in a separate object?