javascript sort object of objects

后端 未结 3 798
无人及你
无人及你 2021-01-27 08:06

I have an Object of Objects. (because I want to use associative array, so object of objects, not numeric array of objects)

var tasks=new Object();

for(...){
tas         


        
3条回答
  •  天涯浪人
    2021-01-27 08:41

    You can pass a function to the sort method of the Array object, which is used to determine the order of the objects in the array. By default, it does this alphabetically and numerically but the numerical way is a bit messed up. Here is how I'd solve your case, assuming that the indexes you're using to sort them are numerical:

    tasks.sort(function(obj1,obj2){
        return obj1.index - obj2.index;
    });
    

    The function passed to the sort method should return a positive integer if object1>object2, 0 if object1=object2, and a negative integer if object1

提交回复
热议问题