Javascript自然排序数组/对象并维护索引关联
我有一个 Javascript 中的项目数组,如下所示:
var users = Array();
users[562] = 'testuser3';
users[16] = 'testuser6';
users[834] = 'testuser1';
users[823] = 'testuser4';
users[23] = 'testuser2';
users[917] = 'testuser5';
我需要对该数组进行排序以获得以下输出:
users[834] = 'testuser1';
users[23] = 'testuser2';
users[562] = 'testuser3';
users[823] = 'testuser4';
users[917] = 'testuser5';
users[16] = 'testuser6';
注意它是如何按数组的值排序的,以及在数组排序后维护值到索引的关联(这很关键)。我一直在寻找解决方案,尝试制作它,但遇到了困难。
顺便说一句,我知道这在技术上不是一个数组,因为这意味着索引总是从 0 到 n 迭代,其中 n+1 是从 n 开始的计数。无论您如何定义,项目的要求仍然是相同的。另外,如果有什么区别的话,我没有使用jquery。
I have an array of items as follows in Javascript:
var users = Array();
users[562] = 'testuser3';
users[16] = 'testuser6';
users[834] = 'testuser1';
users[823] = 'testuser4';
users[23] = 'testuser2';
users[917] = 'testuser5';
I need to sort that array to get the following output:
users[834] = 'testuser1';
users[23] = 'testuser2';
users[562] = 'testuser3';
users[823] = 'testuser4';
users[917] = 'testuser5';
users[16] = 'testuser6';
Notice how it is sorted by the value of the array and the value-to-index association is maintained after the array is sorted (that is critical). I have looked for a solution to this, tried making it, but have hit a wall.
By the way, I am aware that this is technically not an array since that would mean the indices are always iterating 0 through n where n+1 is the counting number proceeding n. However you define it, the requirement for the project is still the same. Also, if it makes a difference, I am NOT using jquery.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
数组元素的顺序由索引定义。因此,即使您以不同的顺序指定值,这些值也将始终按照其索引的顺序存储,并且未定义的索引是
未定义
:现在,如果您想存储索引和值对,你将需要一个不同的数据结构,也许是一个像这样的数组的数组:
这可以使用这个比较函数进行排序:
结果是这个数组:
The order of the elements of an array is defined by the index. So even if you specify the values in a different order, the values will always be stored in the order of their indices and undefined indices are
undefined
:Now if you want to store the pair of index and value, you will need a different data structure, maybe an array of array like this:
This can be sorted with this comparison function:
The result is this array:
如果我正确理解了这个问题,您正在以不应该使用的方式使用数组。事实上,初始化风格
对数组的性质和用途的教导是错误的。数组是项目的有序列表,索引从零开始。创建数组的正确方法是使用数组文字:
索引是根据指定项的顺序隐含的。创建一个数组并设置
users[562] = 'testuser3'
暗示列表中至少有 562 个其他用户,并且您有理由只知道此时第563次。在您的情况下,索引是数据,并不代表集合中项目的顺序。您正在寻找的是地图或字典,在 JavaScript 中由普通对象表示:
现在您的集合没有顺序,但具有有意义的键。从这里,您可以遵循 galambalazs 的建议 创建对象键的数组:
...然后对其进行排序:
这是一个演示< /a>
If I understand the question correctly, you're using arrays in a way they are not intended to be used. In fact, the initialization style
teaches wrong things about the nature and purpose of arrays. An array is an ordered list of items, indexed from zero up. The right way to create an array is with an array literal:
The indexes are implied based on the order the items are specified. Creating an array and setting
users[562] = 'testuser3'
implies that there are at least 562 other users in the list, and that you have a reason for only knowing the 563rd at this time.In your case, the index is data, and is does not represent the order of the items in the set. What you're looking for is a map or dictionary, represented in JavaScript by a plain object:
Now your set does not have an order, but does have meaningful keys. From here, you can follow galambalazs's advice to create an array of the object's keys:
…then sort it:
Here's a demo
你不能在 Javascript 中对数组进行这样的排序。最好的办法是制作一张订单地图。
通过这种方式,您可以独立于原始数组中的键来获得任何您想要的顺序。
要对数组进行排序,请使用自定义排序函数。
[演示]
You can't order arrays like this in Javascript. Your best bet is to make a map for order.
In this way, you can have any order you want independently of the keys in the original array.
To sort your array use a custom sorting function.
[Demo]
根据评论中的想法,我想出了以下解决方案。 naturalSort 函数是我在谷歌上找到的,我修改它来对多维数组进行排序。基本上,我将用户数组设置为多维数组,第一个索引是用户 ID,第二个索引是用户名。所以:
然后我对数组进行排序以获得以下输出:
执行此操作的代码如下:
Using the ideas from the comments, I came up with the following solution. The naturalSort function is something I found on google and I modified it to sort a multidimensional array. Basically, I made the users array a multidimensional array with the first index being the user id and the second index being the user name. So:
I then sorted the array to get the following output:
The code to do this is below:
我会使用一次地图来创建一组新的用户,
然后第二次从新数组中返回所需的字符串。
如果你想避免任何间隙。在输出上使用一个简单的过滤器
I'd use map once to make a new array of users,
then a second time to return the string you want from the new array.
If you want to avoid any gaps. use a simple filter on the output-
稀疏数组通常会带来麻烦。最好将数组中的键值对保存为对象(此技术也是有效的 JSON):
根据建议,您可以使用 for 循环将排序函数映射到数组。
Sparse arrays usually spell trouble. You're better off saving key-value pairs in an array as objects (this technique is also valid JSON):
As suggested, you can use a for loop to map the sorting function onto the array.
Array.prototype.sort()
采用可选的自定义比较函数 - 因此,如果您以这种方式将所有users
转储到数组中[ [562, "testuser3"], [16, "testuser6"] 。 .. 等]
然后使用以下函数对该数组进行
排序
:然后重建您的
users
对象。 (这将使您失去排序。)或者,将数据保留在新排序的数组数组中,如果这适用于您的应用程序。Array.prototype.sort()
takes an optional custom comparison function -- so if you dump all of yourusers
into an array in this manner[ [562, "testuser3"], [16, "testuser6"] ... etc.]
Then
sort
this array with the following function:Then rebuild your
users
object. (Which will loose you your sorting.) Or, keep the data in the newly sorted array of arrays, if that will work for your application.结果为数组数组的 oneliner:
用于按键排序。
用于按值排序。 (适用于原始类型)
A oneliner with array of array as a result:
For sorting by Key.
For sorting by Value. (works with primitive types)