1

I have an array which has list of objects. enter image description here

each value of array looks like : enter image description here

I want to sort name : elements[ele].options.name.

this is what I am trying to do ,but not working for me

           var sortedElements = elements.sort(function (a, b) {
                return a.options.name - b.options.name
            });
            for (var ele in sortedElements) {
                if (sortedElements.hasOwnProperty(ele)) {
                    if (!sortedElements[ele].options.outOfNetworkFlag) {
                        if (sortedElements[ele].options.category === "person") {
                            assignCount++
                        }
                        if (sortedElements[ele].options.category === "unknown") {
                            unassignCount++
                        }
                    }
                    else {
                        oonCount++;
                    }

                    occupancylist += '<a href="#" class="elementLink"><li style="list-style: none" data-elementid="' + sortedElements[ele].options.elementId + '">' + sortedElements[ele].options.icon.options.html + ' ' + sortedElements[ele].options.name + '</li></a>';
                }
            }

Please suggest.

jsBee
  • 413
  • 3
  • 13
  • 1
    You can't subtract a string from another string. To compare strings, use `.localeCompare()`: `return a.options.name.localeCompare(b.options.name);` – Pointy Oct 17 '18 at 13:00
  • Thank you Pointy, this is working 100% – jsBee Oct 17 '18 at 13:03
  • @Pointy Yes you can in most cases sort on ascii. You need localeCompare if there are chars other than `[a-zA-Z0-9]` – mplungjan Oct 17 '18 at 13:19
  • @mplungjan OK yes, I agree that that's often fine, but it remains true that the comparison needs to account for equal sort keys. – Pointy Oct 17 '18 at 13:21
  • 1
    @mplungjan indeed, in my personal experience I don't have to deal with anything other than Latin 1, but `.localeCompare()` is just convenient and I'm pretty sure it won't do anything that seems wrong. – Pointy Oct 17 '18 at 13:22

3 Answers3

4

You can use sort method

  var items = [
      { name: 'saurabh'},
      { name: 'hello'},
      { name: 'jsbee'},
      { name: 'abc'}
    ];

    // sort by name
    items.sort(function(a, b) {
      var nameA = a.name.toUpperCase(); // ignore upper and lowercase
      var nameB = b.name.toUpperCase(); // ignore upper and lowercase
      if (nameA < nameB) {
        return -1;
      }
      if (nameA > nameB) {
        return 1;
      }

      // names must be equal
      return 0;
    });
Saurabh Yadav
  • 3,303
  • 1
  • 10
  • 20
1

You should just use Array.prototype.sort

let src = [{options: {name: 'bbb'}}, {options: {name: 'aaa'}}]
 
src.sort((a, b) => a.options.name > b.options.name ? 1 : -1)

console.log(src);
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
1

Use sort function like this:

objs.sort((a,b) => (a.options.name > b.options.name) ? 1 : ((b.options.name > a.options.name) ? -1 : 0)); 

Of course you can use toUpperCase() (or lower) in sort function.

objs.sort((a,b) => (a.options.name.toUpperCase() > b.options.name.toUpperCase()) ? 1 : ((b.options.name.toUpperCase() > a.options.name.toUpperCase()) ? -1 : 0)); 
JaLe
  • 409
  • 3
  • 13