Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to JSON and I would like to create a (non-anonymous) function that defines the JSON object and the index name to sort by alphabetically. I only code straight-out JavaScript so frameworks don't help me in the least.

var people = [
{'name': 'a75','item1': false,'item2':  false},
{'name': 'z32','item1': true,'item2':  false},
{'name': 'e77','item1': false,'item2': false},
];
share|improve this question
What do you have so far? Why do you explicitly want a non-anonymous function? – pimvdb Nov 17 '11 at 22:12
A non-anonymous function sorting(json_object,key_to_sort_by) {} – John Nov 17 '11 at 22:14
Added the quotes, haven't coded for a few days! Just want to figure out how JSON and JavaScript set the key and then sort. Figure if it's not integer based I could use the sort method perhaps? – John Nov 17 '11 at 22:16
Anonymous function example: window.onload = function() {/* stuff();*/} – John Nov 17 '11 at 22:16
You want a function to sort the array, then? – Jonathan M Nov 17 '11 at 22:17
show 7 more commentsadd comment (requires an account with 50 reputation)

4 Answers

up vote 10 down vote accepted

How about this?

var people = [
    {name: 'a75',
    item1: false,
    item2: false},
    {name: 'z32',
    item1: true,
    item2: false},
    {name: 'e77',
    item1: false,
    item2: false}];

function sortByKey(array, key) {
    return array.sort(function(a, b) {
        var x = a[key]; var y = b[key];
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    });
}

people = sortByKey(people, 'name');

This allows you to specify the key by which you want to sort the array so that you are not limited to a hard-coded name sort. It will work to sort any array of objects that all share the property which is used as they key. I believe that is what you were looking for?

And here is a jsFiddle: http://jsfiddle.net/6Dgbu/

share|improve this answer
Coolbeans, that looks like it works and it's not an anonymous function. Thank you, I'm going to go with this. – John Nov 17 '11 at 22:31
Ah...the visual kicked in, I never would use a,b for two different types which is what had me confused. – John Nov 17 '11 at 22:44
3  
Too bad this doesn't come up first on the gkoberger.github.com/stacksort – vlasits Mar 18 at 20:24
24  
Actually this doesn't work for stacksort (different problems) – roim Mar 18 at 22:12
7  
Not compatible with StackSort. :-( – kba Mar 19 at 3:20
show 1 more commentadd comment (requires an account with 50 reputation)

You can sort an array ([...]) with the .sort function:

var people = [
    {'name': 'a75', 'item1': false, 'item2': false},
    {'name': 'z32', 'item1': true,  'item2': false},
    {'name': 'e77', 'item1': false, 'item2': false},
];

var sorted = people.sort(function IHaveAName(a, b) { // non-anonymous as you ordered...
    return b.name < a.name ?  1 // if b should come earlier, push a to end
         : b.name > a.name ? -1 // if b should come later, push a to begin
         : 0;                   // a and b are equal
});
share|improve this answer
Thanks though that is an annoymous function, I'm trying to do function sort_example(object_name,key_to_sort_by) {} – John Nov 17 '11 at 22:23
@John: It's a named function expression now :) What you want is not too difficult, have a try. – pimvdb Nov 17 '11 at 22:24
add comment (requires an account with 50 reputation)

This isn't a JSON question, per se. Its a javascript array question.

Try this:

people.sort(function(a,b){ 
    var x = a.name < b.name? -1:1; 
    return x; 
});
share|improve this answer
1  
What if they are equal? – Rocket Hazmat Nov 17 '11 at 22:20
I'm trying to do function sort_example(object_name,key_to_sort_by) {} – John Nov 17 '11 at 22:23
add comment (requires an account with 50 reputation)
var people = 
[{"name": 'a75',"item1": "false","item2":"false"}, 
{"name": 'z32',"item1": "true","item2":  "false"}, 
{"name": 'e77',"item1": "false","item2": "false"}]; 

function mycomparator(a,b) {   return parseInt(a.name) - parseInt(b.name);  } 
people.sort(mycomparator); 

something along the lines of this maybe (or as we used to say, this should work).

share|improve this answer
1  
parseInt('a75') is NaN so this won't really work I guess. – pimvdb Nov 17 '11 at 22:25
add comment (requires an account with 50 reputation)

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.