简体   繁体   English

Javascript 中的关联数组排序

[英]Sorting Associative Array in Javascript

I am new to this board have been fishing for info here for a long time and this is my first time posting here.我是这个委员会的新手,很长时间以来一直在这里寻找信息,这是我第一次在这里发帖。 I am in sort of a jam right now I have an array in javascript that looks like this.我现在有点卡住了,我在 javascript 中有一个看起来像这样的数组。 Each ID is accociated with a persons name每个 ID 都带有一个人名

        Array
         (
            [230] => Smith, Marc-Andre 
            [11369] => Smith, Wayne 
            [12561] => Smith, Diane 
            [12637] => Smirnova, Natalie
         )

Right now the array is sorted by the index which is the ID of the person.现在数组按索引排序,索引是人的 ID。 I am wondering if its possible to sort it alphabetically such that the array now looks like我想知道是否可以按字母顺序对其进行排序,以使数组现在看起来像

          Array
          (
             [12637] => Smirnova, Natalia 
             [12561] => Smith, Diane 
             [230] => Smith, Marc-Andre 
             [11369] => Smith, Wayne       
          )

So far I tried using array.sort() it does sort alphabetically but the ID is lost it gives me到目前为止,我尝试使用 array.sort() 它确实按字母顺序排序,但是 ID 丢失了它给了我

      Array
          (
             [0] => Smirnova, Natalia 
             [1] => Smith, Diane 
             [2] => Smith, Marc-Andre 
             [3] => Smith, Wayne       
          )

This is my first time programming javascript so thanks for all the help in advance.这是我第一次编程 javascript 所以提前感谢所有帮助。

Store the entries as objects in an array, with ID and name properties.将条目作为对象存储在数组中,具有 ID 和名称属性。 The JSON would look like: JSON 看起来像:

[ {'id':230,'name':'Smith, Marc-Andre'}, {'id':11369,'name':'Smith, Wayne'}, {'id':12561,'name':'Smith, Diane'}, {'id':12637,'name':'Smirnova, Natalie'} ]

Then pass a function into Array.sort() to choose which property to sort on.然后将 function 传递给 Array.sort() 以选择要排序的属性。 See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/sort请参阅https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/array/sort

You can do something like:您可以执行以下操作:

function arrayify(arg) {
    var ret = [];
    for (var i in arg) {
        if (!arg.hasOwnProperty(i) continue;
        ret.push({key: i, value: arg[i]});
    }
    return ret;
}

arr = arrayify(arr).sort(function(a, b) {
    return a.key.toString() - b.key.toString();
});

You can't use a simple array to do what you're asking for.你不能使用一个简单的数组来做你要求的事情。 An array is, by definition, a list of data that is ordered by the array indexes.根据定义,数组是按数组索引排序的数据列表。 You are asking for it to be ordered by something other than the array indexes.您要求它按数组索引以外的东西排序。 I think the simplest way to store it would end up like this:我认为存储它的最简单方法最终会是这样的:

   var myData = [
        [12637, "Smirnova, Natalia"], 
        [12561, "Smith, Diane"], 
        [230, "Smith, Marc-Andre"], 
        [11369, "Smith, Wayne"]
    ];

So, it would be an array of arrays, where each sub-array contains two items, the first is the id, the second is the name.因此,它将是一个 arrays 的数组,其中每个子数组包含两个项目,第一个是 id,第二个是名称。 This is a similar, but slightly different form what wmorrell proposed.这是 wmorrell 提出的类似但略有不同的形式。

You can then sort the data by the last name by passing a custom comparision into the sort routine like this:然后,您可以通过将自定义比较传递到排序例程中,按姓氏对数据进行排序,如下所示:

// custom sort (assumes all data is fully formed)
// sorts in ascending order by the 2nd item in each embedded array
    myData.sort(function(a,b) {
      if (a[1] < b[1]) return(-1);
      if (a[1] > b[1]) return(1);
      return(0); 
    });

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

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