简体   繁体   English

按值排序Javascript对象

[英]Sorting a Javascript object by value

In my Javascript application I have an Object and I need to be able to order the array by a value within the Inner Object. 在我的Javascript应用程序中,我有一个Object,我需要能够通过内部对象中的值对数组进行排序。

For example: 例如:

{
    a : {
        timestamp: xxxxxx
        other : yyyyyy
    },
    b : {
        timestamp: xxxxxx
        other : yyyyyy
    },
    c : {
        timestamp: xxxxxx
        other : yyyyyy
    }
}

What I need to do is manage this data set and re-order the array according to the timestamp of each inner object. 我需要做的是管理这个数据集并根据每个内部对象的时间戳重新排序数组。

What ways are they that I can do to do this? 他们有什么方法可以做到这一点?

update: 更新:

My initial thought would be doing something like so: 我最初的想法是做这样的事情:

{
    a : {},
    b : {},
    c : {},
    _ : [
        c, a, b //Key's Only
    ]
}

Then re-indexing the the object based on those values, that would sort out how to index the object, but when I insert a new element i would also have to regenerate the _ index relationship which seems way to much effort. 然后根据这些值重新索引对象,这将排序如何索引对象,但是当我插入一个新元素时,我还必须重新生成_索引关系,这似乎很费力。

You can copy the data to an array and then sort it: 您可以将数据复制到数组,然后对其进行排序:

var data = {
    a : {
        timestamp: 11111,
        other : "xxx"
    },
    b : {
        timestamp: 22222,
        other : "yyy"
    },
    c : {
        timestamp: 33333,
        other : "zzz"
    }
};

var output = [];

// copy items to an array so they can be sorted
for (var key in data) {
    data[key].key = key;   // save key so you can access it from the array (will modify original data)
    output.push(data[key]);
}    

output.sort(function(a,b) {
    return(a.timestamp - b.timestamp);
});

Generates this as output (note I added the original key to the object so it's accessible from the array): 生成它作为输出(注意我将原始键添加到对象,以便可以从数组访问):

[{"timestamp":11111,"other":"xxx","key":"a"},
{"timestamp":22222,"other":"yyy","key":"b"},
{"timestamp":33333,"other":"zzz","key":"c"}]

You can see this working here: http://jsfiddle.net/jfriend00/hXpkP/ 你可以在这里看到这个: http//jsfiddle.net/jfriend00/hXpkP/

Javascript objects are not associative arrays. Javascript对象不是关联数组。 They may behave similarly, but they are not the same. 它们可能表现相似,但它们不一样。 Javascript has no associative arrays. Javascript没有关联数组。

While associative arrays have a concept of order, Javascript objects simply do not share this particular feature with them. 虽然关联数组具有顺序概念,但Javascript对象根本不与它们共享此特定功能。 Objects, by there very nature, are not ordered. 物体,非常本质,没有订购。

So, to answer your question: you cannot order them... 所以,回答你的问题:你不能订购它们......

you are not dealing with an array, but an object with property values of a , b , and c 您没有处理数组,而是处理属性值为abc

there is no reason you would need them in a specific order, as you can't really loop over them in any particular order. 你没有理由按特定的顺序需要它们,因为你无法以任何特定的顺序循环它们。

this would be trivial (using sort ) if you were working with an array... 这将是微不足道的(使用sort )如果使用数组...

var array = [
    {
        timestamp: xxxxxx
        other : yyyyyy
    },
    {
        timestamp: xxxxxx
        other : yyyyyy
    },
    {
        timestamp: xxxxxx
        other : yyyyyy
    }
];

array.sort(function(a,b) {
    return a.timestamp - b.timestamp;
});

As the others have already said, you are dealing with an associative object. 正如其他人已经说过的那样,你正在处理一个关联对象。 Not an array. 不是数组。 Objects don't have order. 对象没有订单。

If you wish to keep it as-is, and sort an array of the keys instead, you can do this: 如果您希望保持原样,并对键的数组进行排序,则可以执行以下操作:

var obj = {
    a : {
        timestamp: xxxxxx
        other : yyyyyy
    },
    b : {
        timestamp: xxxxxx
        other : yyyyyy
    },
    c : {
        timestamp: xxxxxx
        other : yyyyyy
    }
};

var keys = [];
for(var key in obj) {
    keys.push(key);
}

keys.sort(function(a, b) {
    return obj[a].timestamp - obj[b].timestamp;
});

Now you can access the object via the array values (eg obj[keys[0]], obj[keys[1]], etc). 现在您可以通过数组值访问对象(例如obj [keys [0]],obj [keys [1]]等)。 This assumes timestamps are numeric. 这假定时间戳是数字。 If they are date objects the sort should be: 如果它们是日期对象,则排序应为:

keys.sort(function(a, b) {
    return +obj[a].timestamp - (+obj[b].timestamp);
});

If the timestamps are actually strings representing a date time (like 'August 2, 2012'), then it should be: 如果时间戳实际上是表示日期时间的字符串(如'2012年8月2日'),那么它应该是:

keys.sort(function(a, b) {
    return +new Date(obj[a].timestamp) - (+new Date(obj[b].timestamp));
});

So use whatever makes most sense in your scenario. 因此,在您的场景中使用最有意义的内容。

You could create a custom comparer function and use the built-in sort function for arrays. 您可以创建自定义比较器函数并对数组使用内置排序函数。 See this post . 看这篇文章

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

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