-1

How to sort by key "name"? (in alphabetical order)

a busy cat

It is not working:

views.sort(function(a, b) {
  var x = a.name.toLowerCase(),
      y = b.name.toLowerCase();
  return ((x < y) ? -1 : ((x > y) ? 1 : 0));
})

Please help me.

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
user3608884
  • 59
  • 1
  • 6
  • 2
    Define "not working". Be specific about what you expect vs what it's giving you. And does `sort` return the sorted list or modify it in place? I can't remember. – Carcigenicate Sep 27 '17 at 21:56
  • If that is a log of your `views` variable, that isn't an array, it's a regular object. It won't have a `sort()` function, nor can you sort an object's properties – Patrick Evans Sep 27 '17 at 22:02
  • @Carcigenicate My code does not the sorting. It just doesn't work. I think all right, but it's not working. – user3608884 Sep 27 '17 at 22:04
  • 1
    Your sort function is correct, The problem is somewhere else, please give more code about your objects you're sorting. – Ammar Sep 27 '17 at 22:07
  • can you post sample array, i think you are not getting array name – nikunjM Sep 27 '17 at 22:08
  • How does `views.sort((a, b) => a.name.toLowerCase() > b.name.toLowerCase())` not work? – Oluwafemi Sule Sep 27 '17 at 22:20
  • @nikunjMnage it is not array. It is object http://take.ms/9Y73o – user3608884 Sep 27 '17 at 22:25
  • It's going to take me a while to sort an image, be much easier if was in JSON, or a javascript object literal / array.. – Keith Sep 27 '17 at 22:31
  • @Keith I did it myself Object.values(views) .sort(function(a, b) { var x = a.name, y = b.name; return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }) – user3608884 Sep 27 '17 at 22:37
  • Crikey!, you have created an algorithm to sort an image,. in such short lines. And I thought you might need some complicated OCR, as the fist part to extract the text out of the image. Well I never..!!! – Keith Sep 27 '17 at 22:39
  • [Do not post images of code or errors!](https://meta.stackoverflow.com/q/303812/995714) – Rob Oct 01 '17 at 17:56

4 Answers4

0

You an use localeCompare for comparing strings:

var views = {
  viewA: { name: 'Build Plus' },
  viewB: { name: 'Columns' },
  viewC: { name: 'PowerPack Templates', other: 'field' },
  viewD: { name: 'Blog Posts' }
};

var viewsArr = Object.keys(views).map(function (key) { return views[key]; });

viewsArr.sort(function(a, b) {
      var x = a.name.toLowerCase(),
          y = b.name.toLowerCase();
      return x.localeCompare(y);
    })
    
console.log(viewsArr);

Update: You are receiving object with keys and values. You need to take values in the array first and then sort them.

See Converting a JS object to an array using jQuery

In your case it will be:

var viewsArr = Object.keys(views).map(function (key) { return views[key]; });
// sort then
Samich
  • 29,157
  • 6
  • 68
  • 77
  • That should work, I put example of your data in the code snippet - take a look – Samich Sep 27 '17 at 22:18
  • Oh, I know. you cannot sort them because you have an object with keys which cannot be sorted. You need to convert it first in array. – Samich Sep 27 '17 at 22:19
  • @user3608884 actually, it looks weird: on the screenshot you've posted you have collection but then each item in collection has a key (like object). Can you post what is your object/collection look like? – Samich Sep 27 '17 at 22:29
  • thanks, but my solution: `var views_new = {}; Object.values(views) .sort(function(a, b) { var x = a.name, y = b.name; return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }).forEach(function(v, i) { views_new[v.handle] = v; });` – user3608884 Sep 27 '17 at 22:46
  • Glad you was able to figure it out. – Samich Sep 27 '17 at 22:59
0

Your code that you shared is totally OK, here is the working example:

var arr = [
  {name: 'b'},
  {name: 'a'},
  {name: 'x'},
  {name: 'i'},
  {name: 'l'}
]

arr.sort(function(a, b) {
 var x = a.name.toLowerCase(),
     y = b.name.toLowerCase();
 return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});

console.log(arr)

For more info about sort: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

May be the issue is somewhere else, in code, which you didn't share.

P.S.
  • 15,970
  • 14
  • 62
  • 86
  • @PatrickEvans, yes, I just added working example, which the OP can look through and see, how it's written – P.S. Sep 27 '17 at 22:04
  • That doesn't help teach OP or other users what is wrong and what was needed to fix the issue. – Patrick Evans Sep 27 '17 at 22:06
  • @PatrickEvans, OP's code is totally OK, may be the issue is somewhere else, in code, which OP didn't share. – P.S. Sep 27 '17 at 22:08
  • Correct... so your answer doesn't answer anything... which is my point – Patrick Evans Sep 27 '17 at 22:08
  • @PatrickEvans, I wrote my answer based on OP's shared code, and said, that His/Her shared code is totally OK, and the issue may be somewhere else. – P.S. Sep 27 '17 at 22:12
  • Yes meaning you should have just made a comment about that on the question. This answer provides nothing for OP or future users – Patrick Evans Sep 27 '17 at 22:22
  • @PatrickEvans, the main goal of my answer is to show, that shared by OP code is working with my data – P.S. Sep 27 '17 at 22:24
  • @user3608884, where the issue was? – P.S. Sep 27 '17 at 22:49
  • @CommercialSuicide my solution: `var views_new = {}; Object.values(views) .sort(function(a, b) { var x = a.name, y = b.name; return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }).forEach(function(v, i) { views_new[v.handle] = v; });` – user3608884 Sep 27 '17 at 22:54
  • @user3608884, nice to hear, that you founded solution! – P.S. Sep 27 '17 at 22:56
0

Get object values in an array and sort.

const viewsArr = Object.values(views)
             .sort((a, b) => a.name.lowerCase() > b.name.lowerCase());
Oluwafemi Sule
  • 36,144
  • 1
  • 56
  • 81
-1

This is not exact code but in your you are not getting groups..

 let groups = Object.keys(data)
    .map(group => {
let records = Object.values(data[group])
        .map(record => ({
          name: record.name,
          group,
        }));
      return records;
    });
groups = [].concat(...groups);
  return groups.sort((a, b) => {
   return a.name.localeCompare(b.name);
});
nikunjM
  • 560
  • 1
  • 7
  • 21
  • 1
    variables can be declared in a list, ie `var a=3,b=4,c=4;` which is what OP is doing, they just put the second declaration on a different line – Patrick Evans Sep 27 '17 at 22:05