最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sorting - Javascript sort multidimensional object - Stack Overflow

programmeradmin3浏览0评论

So I know that there are dozens of posts on here that describe how to sort a multidimensional object, but I have not been able to find one that fits my needs.

All those solutions are based on an object like this:

[{id: 1, value: 'aaa'},{id: 40, value: 'bbbb'}]

My object looks like this:

buttons: {
    confirm: {
        class: 'btn btn-primary',
        value: 'Opslaan',
        order: 1
    },
    cancel: {
        class: 'btn btn-default',
        value: 'Annuleer',
        order: 10
    }
    delete: {
        class: 'btn btn-danger',
        value: 'Verwijder',
        order: 2
    }
},

Naturally, I want to sort this so that the oute is: confirm, delete, cancel. I have tried this (which I expected not to work):

this.options.buttons.sort ( function ( a, b ) {
    return a.order - b.order;
});

But that gave me an Uncaught TypeError: undefined is not a function

Any help is much appreciated!

So I know that there are dozens of posts on here that describe how to sort a multidimensional object, but I have not been able to find one that fits my needs.

All those solutions are based on an object like this:

[{id: 1, value: 'aaa'},{id: 40, value: 'bbbb'}]

My object looks like this:

buttons: {
    confirm: {
        class: 'btn btn-primary',
        value: 'Opslaan',
        order: 1
    },
    cancel: {
        class: 'btn btn-default',
        value: 'Annuleer',
        order: 10
    }
    delete: {
        class: 'btn btn-danger',
        value: 'Verwijder',
        order: 2
    }
},

Naturally, I want to sort this so that the oute is: confirm, delete, cancel. I have tried this (which I expected not to work):

this.options.buttons.sort ( function ( a, b ) {
    return a.order - b.order;
});

But that gave me an Uncaught TypeError: undefined is not a function

Any help is much appreciated!

Share Improve this question asked May 6, 2014 at 16:04 DiederikDiederik 6129 silver badges24 bronze badges 3
  • 2 It is as simple as: An object does not have sort() and objects do not guarantee order. – epascarello Commented May 6, 2014 at 16:05
  • Sometimes this website make me feel like an idiot.. haha. But does that mean that there is no way to achieve my oute? Except for changing it to an array? – Diederik Commented May 6, 2014 at 16:07
  • 1 @DiederikvandenB Technically, there are probably ways you could sort an object (Object.keys, which will then let you sort that array, and then you could reconstruct your object). However, objects are unsorted based on the ECMA spec (see: ecma-international/publications/standards/Ecma-262.htm) so no guarantee can be made that the object will stay sorted to your liking. Your far better off converting the buttons property to an array. – Jason L. Commented May 6, 2014 at 16:15
Add a ment  | 

2 Answers 2

Reset to default 5

As @epascarello mentions, there is no native sort function for objects in JavaScript.

If you are interested in sorting an array of your object's keys according to some order, you can use Object.keys()!

var buttons = {
  confirm: { value: 'foo', order: 0 },
  cancel: { value: 'bar', order: 2},
  delete: { value: 'baz', order: 1}
};

var sortedButtons = Object.keys(buttons).sort( function(keyA, keyB) {
  return buttons[keyA].order - buttons[keyB].order;
}); // returns ['confirm', 'delete', 'cancel']

I had a very similar need and I ended up doing something along these lines --

// Code typed on the fly -- untested.
buttons: {
    list: [],  // We'll store things here.
    confirm: {
        class: 'btn btn-primary',
        value: 'Opslaan',
        order: 1
    },
    cancel: {
        class: 'btn btn-default',
        value: 'Annuleer',
        order: 10
    }
    delete: {
        class: 'btn btn-danger',
        value: 'Verwijder',
        order: 2
    }
}


// Now, fill the list from the properties

for (var key in buttons) {
  // Don't actually use the 'list' property in the output
  if (key !== "list") {
    list.push(key);
  }
}

// Now that we have our list, sort it
// Any specialized sorting can be done in the `sort` method here
buttons.list = buttons.list.sort();

// Now, deal with the sorted list
for (var i = 0; i < buttons.list.length; i++) {
  var key = buttons.list[i];
  var button = buttons[key];
  console.log(button);
}
发布评论

评论列表(0)

  1. 暂无评论