简体   繁体   English

如何按数字升序对对象数组进行排序?

[英]How to sort an array of objects in ascending order of number?

I have an array of objects like the following : 我有一个类似以下对象的数组:

var array = {
    "112" : {
             "id": "3",
             "name": "raj"
    },
    "334" : {
             "id": "2",
             "name": "john"
    },
    "222" : {
             "id": "5",
             "name": "kelvin"
    }
}

Now i want to sort the array in ascending order of id and then restore it in array. 现在,我想按id升序对数组进行排序,然后将其还原到数组中。 I tried using sort() but could not do it. 我尝试使用sort()但无法执行。 Please help how to do so that when i display the data from the array it comes sorted. 请帮助该怎么做,以便当我显示数组中的数据时将其排序。

var array = {
    "112" : {
         "id": "3",
         "name": "raj"
    },
    "334" : {
         "id": "2",
         "name": "john"
    },
    "222" : {
         "id": "5",
         "name": "kelvin"
    }
}
var sortedObject = Array.prototype.sort.apply(array);

result:
{
    "112": {
        "id": "3",
        "name": "raj"
    },
    "222": {
        "id": "5",
        "name": "kelvin"
        },
    "334": {
        "id": "2",
        "name": "john"
    }
}

That isn't an array, it is an object (or would it if it wasn't for the syntax errors ( = should be : )). 这不是一个数组,它是一个对象(或将它,如果它不是为语法错误( =: ))。 It doesn't have an order. 它没有命令。

You could use an array instead (making the current property names a value of a key on the subobjects). 您可以改用数组(使当前属性名称成为子对象上键的值)。

Alternatively, you could use a for loop to build an array of the key names, then sort that and use it as a basis for accessing the object in order. 或者,您可以使用for循环构建键名数组,然后对其进行排序并将其用作按顺序访问对象的基础。

That is an object but you can sort an array ilke this: 那是一个对象,但是您可以像这样对数组进行排序:

Working Demo: http://jsfiddle.net/BF8LV/2/ 工作演示: http : //jsfiddle.net/BF8LV/2/

Hope this help, 希望有帮助

code

   function sortAscending(data_A, data_B)
{
    return (data_A - data_B);
}
var array =[ 9, 10, 21, 46, 19, 11]
array.sort(sortAscending) 
alert(array);​

JavaScript objects are unordered by definition. 根据定义,JavaScript对象是无序的。 The language specification doesn't even guarantee that, if you iterate over the properties of an object twice in succession, they'll come out in the same order the second time. 语言规范甚至不能保证,如果您连续两次遍历对象的属性,那么第二次它们将以相同的顺序出现。

If you need things to be ordered, use an array and the Array.prototype.sort method. 如果需要订购商品,请使用数组和Array.prototype.sort方法。

Assuming you meant your code to be an array of objects, ie: 假设您的代码是对象数组,即:

var unsortedArray = [
    { id: 3, name: "raj" },
    { id: 2, name: "john" },
    { id: 5, name: "kelvin" } 
];

Then you would be able to sort by id by passing a function to Array.sort() that compares id's: 然后,您可以通过将比较ID的函数传递给Array.sort()来按ID进行排序:

var sortedArray = unsortedArray.sort(function(a, b) {
    return a.id - b.id 
});

As others have pointed out, what you have is an object containing objects, not an array. 正如其他人指出的那样,您拥有的是一个包含对象的对象,而不是数组。

Not many people knows that Array.sort can be used on other kinds of objects, but they must have a length property: 没有多少人知道Array.sort 可以用于其他类型的对象,但是它们必须具有length属性:

array.length = 334;
Array.prototype.sort.call(array, function(a, b) {return a.id - b.id;});

Unfortunately, this doesn't work well if your "array" is full of "holes" like yours. 不幸的是,如果您的“数组”充满了像您一样的“漏洞”,那么这将无法正常工作。

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

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