0

Im trying to sort the items from an object/array by their "name" item,

I used this page for reference Sort array of objects and build the piece of code below:

var alphabet = {
    a: 1,
    b: 2,
    c: 3,
    d: 4,
    e: 5,
    f: 6,
    g: 7,
    h: 8,
    i: 9,
    j: 10,
    k: 11,
    l: 12,
    m: 13,
    n: 14,
    o: 15,
    p: 16,
    q: 17,
    r: 18,
    s: 19,
    t: 20,
    u: 21,
    v: 22,
    w: 23,
    x: 24,
    y: 25,
    z: 26
}

var test = {
    item: {
        name: "Name here",
        email: "[email protected]"
    },

    item2: {
        name: "Another name",
        email: "[email protected]"
    },

    item3: {
        name: "B name",
        email: "[email protected]"
    },

    item4: {
        name: "Z name",
        email: "[email protected]"
    }
};
test.sort(function (a, b) {return alphabet[a.name.charAt(0)] - alphabet[b.name.charAt(0)]});

console.log(test);

Unfortunately no errors are returned and the console.log doesn't returns anything either. Any help is greatly appreciated!

EDIT: After the answer has been given, it seemed the variable "test" was required to be an array, however, the variable was generated dynamically in an external library, therefor I made this little piece of code. If anyone has the same issue, feel free to use it.

var temp = [];
$.each(test, function(index, value){
    temp.push(this);
});

//temp is the resulting array
2
  • 2
    There is no sort method for Objects--only Arrays! If you want to sort the object, your best bet is probably to map the keys in test to an Array and call sort from there. Commented Apr 15, 2012 at 23:34
  • You are only sorting by the first letter. Multiple names with the same first letter will sort semi-randomly. Is that what you want? Commented Apr 15, 2012 at 23:50

2 Answers 2

4

test is an object, not an array. Perhaps you want this:

var test = [
    {
        name: "Name here",
        email: "[email protected]"
    },
    ⋮
];

If you need the item, item1, … to be retained against each object, you could add them as fields of each object:

var test = [
    {
        id: "item",
        name: "Name here",
        email: "[email protected]"
    },
    ⋮
];

To sort alphabetically, you need a case-insensitive comparator (and forget the alphabet object):

compareAlpha = function(a, b) {
  a = a.name.toUpperCase(); b = b.name.toUpperCase();
  return a < b ? -1 : a > b ? 1 : 0;
};
Sign up to request clarification or add additional context in comments.

2 Comments

isnt the compareAlpha function doing almost the same as the current marked answer?
@xorinzor: No, it's comparing the entire string (not just the first character) and doesn't rely on an alphabet object.
1

Firstly, test should be an array, not an object. Secondly, I think you are missing a call to .toLowerCase() after selecting the character.

test.sort(function (a, b) {
    return alphabet[a.name.charAt(0).toLowerCase()] - alphabet[b.name.charAt(0).toLowerCase()];
});

1 Comment

Ah yes, I indeed forgot the .toLowerCase() function, it is working perfect now!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.