简体   繁体   English

重新排序数组而不加扰原始排序

[英]Re-sorting an array without scrambling the original sort

I have an array of employee information that is sorted by comparison of the employee's sub department to the priority of sub-departments within a given department. 我有一系列员工信息,这些信息通过比较员工的子部门与给定部门中子部门的优先级进行排序。

So when an employees name is clicked in an index, their department (stored as a data attribute) is used to grab all other employees sharing that department, and this collection is sorted by the subdepartment priority as-set manually in another array 因此,当在索引中单击员工姓名时,其部门(存储为数据属性)将用于捕获共享该部门的所有其他员工,并且此集合将按在另一个数组中手动​​设置的子部门优先级进行排序

var results = $.grep(data, function(employee) {
    if(employee.DEPT == dept) {
        return employee.DEPT = dept;
    }
});

// ** NOTE 'deptorder' is "Department" : [ "Subdepartment1", "Subdepartment2" ] defined manually
// pick the order of subdepts based on the dept
order = deptorder[dept];

The first sort: 第一种:

results.sort(function(a, b) {
    return $.inArray(a.SUBDEPT, order) - $.inArray(b.SUBDEPT, order);
});

I then go through the sorted array and create a grid of employee info, with section headers dividing the employees by their sub department. 然后,我遍历排序后的数组,并创建一个员工信息网格,各部分的标题将员工除以其子部门。

Problem: I need to arrange the employees within each sub department AFTER they have been sorted into sub department groups. 问题:在将员工划分为子部门组之后,我需要在每个子部门中安排员工。 I'm open to suggestions on how to do this, but all I need is to flag a 'manager' and make them first in the grid for their given sub department. 我愿意就如何做到这一点提出建议,但我所需要的只是标记一个“经理”,并将其首先放在给定子部门的网格中。 After the manager is rendered the rest can fall to alphabetical, or whatever. 经理出任后,其余人员可以按字母顺序或其他方式使用。

How can I sort the array a second time to achieve this? 如何第二次对数组进行排序以实现此目的? Right now managers have an employee.MANAGER attribute '1', while the rest have ''. 现在,经理有一个employee.MANAGER属性'1',其余的则有'。

I've tried 我试过了

results.sort(function(a, b) {
    return (a.MANAGER == 1) - b;
});

but this doesn't work. 但这不起作用。 The sorting shown above is my first and only experience with sort. 上面显示的排序是我的第一个也是唯一的排序经验。 Any help appreciated! 任何帮助表示赞赏!

You can do all the comparisons in a single .sort() pass: 您可以在一个.sort()传递中进行所有比较:

results.sort(function(a, b) {
  if (a.SUBDEPT == b.SUBDEPT) {
    if (a.MANAGER == 1)
      return -1; // a goes first
    if (b.MANAGER == 1)
      return 1; // b goes first
    return 0; // or compare names
  }
  return $.inArray(a.SUBDEPT, order) - $.inArray(b.SUBDEPT, order);
}

In other words, while sorting, if you notice that you're comparing two elements in the same department, you check to see if either is the manager. 换句话说,在排序时,如果您发现您正在比较同一部门中的两个元素,则需要检查其中一个是否是经理。 If so, that one should go before the other one. 如果是这样,那一个应该先于另一个。 If they're in different departments, then that's all that matters. 如果他们在不同的部门,那就很重要。

If they're in the same department but neither is a manager, you can then do the comparison by name or anything else you like. 如果他们在同一个部门中,但都不是经理,则可以按名称或您喜欢的其他方式进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM