简体   繁体   English

为什么 javascript 数组排序不会将所有元素相互比较?

[英]Why javascript array sort doesn't compare all elements with each other?

I'm trying to understand why Javascript array sort doesn't work with the following logic.我试图理解为什么 Javascript 数组排序不适用于以下逻辑。 I have no problems making my own algorithm to sort this array, but I'm trying to make it with the Javascript sort built-in method to understand it better.我自己的算法对这个数组进行排序没有问题,但我正在尝试使用 Javascript 排序内置方法来更好地理解它。

In this code, I want to push entities that "belongs to" another entity to the bottom, so entities that "has" other entities appear on the top.在这段代码中,我想将“属于”另一个实体的实体推到底部,以便“拥有”其他实体的实体出现在顶部。 But apparently, the sort method doesn't compare all elements with each other, so the logic doesn't work properly.但显然, sort 方法不会将所有元素相互比较,因此逻辑无法正常工作。

Am I doing something wrong, or it is the correct behavior for the Javascript sort method?我做错了什么,还是 Javascript 排序方法的正确行为?

The code I'm trying to execute:我试图执行的代码:

 let entities = [ { name: 'Permission2', belongsTo: ['Role'] }, { name: 'Another', belongsTo: ['User'] }, { name: 'User', belongsTo: ['Role', 'Permission2'] }, { name: 'Teste', belongsTo: ['User'] }, { name: 'Role', belongsTo: ['Other'] }, { name: 'Other', belongsTo: [] }, { name: 'Permission', belongsTo: ['Role'] }, { name: 'Test', belongsTo: [] }, ] // Order needs to be Permission, let sorted = entities.sort((first, second) => { let firstBelongsToSecond = first.belongsTo.includes(second.name), secondBelongsToFirst = second.belongsTo.includes(first.name) if(firstBelongsToSecond) return 1 if(secondBelongsToFirst) return -1 return 0 }) console.log(sorted.map(item => item.name))

As you can see, "Role" needs to appear before "User", "Other" before "Role", etc, but it doesn't work.如您所见,“角色”需要出现在“用户”之前,“其他”出现在“角色”之前,等等,但它不起作用。

Thanks for your help!谢谢你的帮助! Cheers干杯

You're running into literally how sorting is supposed to work: sort compares two elements at a time, so let's just take some (virtual) pen and paper and write out what your code is supposed to do.从字面上看,排序应该如何工作: sort一次比较两个元素,所以让我们拿一些(虚拟的)笔和纸,写下你的代码应该做什么。

If we use the simplest array with just User and Role, things work fine, so let's reduce your entities to a three element array that doesn't do what you thought it was supposed to do:如果我们使用包含 User 和 Role 的最简单数组,一切正常,所以让我们将您的entities简化为一个三元素数组,它不会做您认为应该做的事情:

let entities = [
  {
    name: 'User',
    belongsTo: ['Role', 'Permission2']
  },
  {
    name: 'Test',
    belongsTo: []
  },
  {
    name: 'Role',
    belongsTo: ['Other']
  }
]

This will yield {User, Test, Role} when sorted, because it should ... so let's see why it should:这将在排序时产生 {User, Test, Role} ,因为它应该......所以让我们看看它为什么应该:

  1. pick elements [0] and [1] from [user, test, role] for comparison从 [user, test, role] 中选择元素 [0] 和 [1] 进行比较
  2. compare(user, test)比较(用户,测试)
    • user does not belong to test用户不属于测试
    • test does not belong to user测试不属于用户
    • per your code: return 0, ie don't change the ordering根据您的代码:返回 0,即不要更改排序
  3. we slide the compare window over to [1] and [2]我们将比较 window 滑动到 [1] 和 [2]
  4. compare(test, role)比较(测试,角色)
    • test does not belong to role测试不属于角色
    • role does not belong to test角色不属于测试
    • per your code: return 0, ie don't change the ordering根据您的代码:返回 0,即不要更改排序
  5. we slide the compare window over to [2] and [3]我们将比较 window 滑动到 [2] 和 [3]
    • there is no [3], we're done没有 [3],我们完成了
  6. The sorted result is {user, test, role}, because nothing got reordered排序结果是 {user, test, role},因为没有重新排序

So the "bug" is thinking that sort compares everything-to-everything: as User and Role are not adjacent elements, they will never get compared to each other.所以“错误”认为sort会比较一切:因为用户和角色不是相邻的元素,它们永远不会被相互比较。 Only adjacent elements get compared.仅比较相邻元素。

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

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