Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I'm trying to sort this array on vCount (descending) but I can't figure out how to do so. I've searched stack overflow but I can't find anything similar to my issue.

This is what I came up with this far, I guess this doesn't work because vCount is deeper in the array but how do I get deeper in the array within the sort function?

var nar = trck_urls.sort(function(a, b){ // trck_urls is the array
  return b['vCount'] - a['vCount'];
});

This is the array I'm trying to sort. (console.log output)

[ '/home': [ pageTitle: 't1',
    Visitors: [ 'oPc-Gr6SYxN1AMyw8Pst' ],
    vCount: 1 ],
  '/dash': [ pageTitle: 't2',
    Visitors: [ 'jepoSdBR9_ur3XSu8Psu', 'WIO4fEt1Ue8yHCly8Psv' ],
    vCount: 2 ] ]
share|improve this question
    
array has invalid object syntax in it, can't sort an object – charlietfl May 25 '14 at 20:11
    
FYI About objects sorting – Dalorzo May 25 '14 at 20:31
    
If that's the output of console.log, you've abused JavaScript arrays. If it's your script, then you've got a syntax error. – Bergi May 25 '14 at 20:42
    
@Dalorzo thats not a issue because it's for Node.JS but thanks for mentioning it. – Tim K May 25 '14 at 20:46
    
@Bergi console.log output yes. Will read that article later when I have some more time. – Tim K May 25 '14 at 20:48

2 Answers 2

up vote 0 down vote accepted

I suggest you coerce your data structure into something easily sortable like this:

var trck_urls = [
  { path:'/home',
    pageTitle: 't1',
    Visitors: ['oPc-Gr6SYxN1AMyw8Pst'],
    vCount: 1 },
  { path:'/dash',
    pageTitle: 't2',
    Visitors: ['jepoSdBR9_ur3XSu8Psu', 'WIO4fEt1Ue8yHCly8Psv' ],
    vCount: 2 }
];

var sorted = trck_urls.sort(function(a,b){
    return b.vCount - a.vCount;
});

If you need to store the data as an object, you can coerce the object into an array whenever you need to sort it, like this:

var sortUrls = function(dict){
  return Object.keys(dict).map(function(key){
      var item = dict[key];
      item.path = key;
      return item;
  }).sort(function(a, b){
      return a.vCount - b.vCount;
  });
}

var trck_urls = {
  '/home' : { 
      pageTitle: 't1',
      Visitors: ['oPc-Gr6SYxN1AMyw8Pst'],
      vCount: 1 },
  '/dash' : {
      pageTitle: 't2',
      Visitors: ['jepoSdBR9_ur3XSu8Psu', 'WIO4fEt1Ue8yHCly8Psv' ],
      vCount: 2 }
};

var sortedUrls = sortUrls(trck_urls);  

In this case, Object.keys() gives you the list

['/home', '/dash']

and map lets you replace each of those keys with the corresponding data, thus coercing the original object to an array. Then the array can be sorted.

share|improve this answer
    
Hi Eric, I like a structure like this however I need to be able to get the vCount, Visitors and pageTitle by path. Thats why I chose to use the path as key. Or is there another way to get the values by path with this structure? – Tim K May 25 '14 at 20:44
    
I've updated my answer to show how you could store the data in an object, where the path is the key, and coerce that object to an array before sorting. – Eric May 27 '14 at 7:45

From the point of view of the sort function, what you are trying to sort is actually an empty array with a bunch of additional properties (/home, /dash). So if you want to sort them, you need to first create an array where the elements are the properties of your original array:

var array = [];
for (var key in nar) {
    array.push({key:key, value:nar[key]});
}
array.sort(function(a,b){
    return b.value.vCount - a.value.vCount;
}
share|improve this answer
    
Thanks Noziar. This works partially, unfortunately it breaks the Visitor array. – Tim K May 25 '14 at 20:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.