Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting array based on custom priority of one attribute
    primarykey
    data
    text
    <p>I'm using jQuery to parse a JSON file of employees, which contains their name, department, subdepartment, and some other details.</p> <p>e.g.:</p> <pre><code>[ { "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" }, ] </code></pre> <p>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.</p> <p>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."</p> <p>note: dept = data-dept passed by jquery selector..</p> <pre><code>$.each(data, function(i, employee) { if (employee.DEPT == dept) { var employeetext = '&lt;span class="name"&gt;' + employee.FIRST+' '+ employee.LAST+'&lt;/span&gt;', employee.JOINED, employee.TITLE, employee.RESIDENCE, '...', employee.HOBBIES; $('#employees').append('&lt;div class="employee_block"&gt;&lt;img src="" width="85" height="113"&gt;' + employeetext + '.&lt;/div&gt;'); results.push(employee); } }) // end stepping through employees </code></pre> <p>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:</p> <pre><code>var subdeptorder = [ { "DEPT": "Marketing", "SUBDEPTS": ["Administration", "Purchasing", "Advertising", "PR"] }, { "DEPT": "Sales", "SUBDEPTS": ["Administration", "Business Development"] } ] </code></pre> <p>So I need to sort the "results" array according to the employee's department (and that department's subdepartment order) within it.</p> <p>How do I write a comparison function to re-sort the "results" array according to a priority established in a separate object? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload