0

I'd like to be able to sort an array an array of objects in JavaScript based on the first word in the string. Currently the sort function I'm using uses all words in the string.

JSON data:

"awds": [
    {"awd":"adobe edge award"},
    {"awd":"besty"},
    {"awd":"c award"},
    {"awd":"awwward award"},
    {"awd":"desk award"},
    {"awd":"adobe edge award"},
    {"awd":"creative edge award"},
    {"awd":"snoogle edge award"},
    {"awd":"scuba edge award"},
    {"awd":"xidoe edge award"}
]

JavaScript:

var compareNames = function(a, b) {
    var nameA = a.awd.toLowerCase();
    var nameB = b.awd.toLowerCase();
    if (nameA > nameB) { return 1; }
    return 0;
};

Current Output:

adobe edge award
besty
c award
awwward award
desk award
adobe edge award
creative edge award
snoogle edge award
scuba edge award
xidoe edge award

Desired Output:

adobe edge award
adobe edge award
awwward award
c award
creative edge award
scuba edge award
snoogle edge award
xidoe edge award
2
  • It doesn't work at all! Commented Feb 11, 2013 at 17:39
  • How about using the substring function? Commented Feb 11, 2013 at 17:40

4 Answers 4

1
var o = [
    {"awd":"adobe edge award"},
    {"awd":"besty"},
    {"awd":"c award"},
    {"awd":"awwward award"},
    {"awd":"desk award"},
    {"awd":"adobe edge award"},
    {"awd":"creative edge award"},
    {"awd":"snoogle edge award"},
    {"awd":"scuba edge award"},
    {"awd":"xidoe edge award"}
];

o.sort(function(a, b) {
    var nameA = a.awd.toLowerCase();
    var nameB = b.awd.toLowerCase(); 
    if(nameA === nameB) return 0; 
    return nameA > nameB ? 1 : -1;
});

console.log(o);

Demo

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Array.sort with compare function. See https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort

2 Comments

Thats exactly what I'm using and the output is not what I want. content.awds.sort(compareNames);
Rigth, the other answer given meanwhile points out your mistake. You need to return not just 1 or 0 but -1 too. (0 for equal)
1
  1. The sort callback should return negative, 0, positive number depending on whether argument 1 is less than, equal to, or greater than argument 2
  2. Sorting is done on the original so you do not have to "assign" the result
  3. You can use regex to extract the first word (I don't see why you want to sort that way)
awds.sort(function (a, b) {
    var aword = a.awd.toLowerCase().match(/[^ ]+/)[0];
    var bword = b.awd.toLowerCase().match(/[^ ]+/)[0];
    return aword == bword ? 0 : (aword < bword ? -1 : 1);
});

Comments

0

Your method will work if you return -1 when the first item is not greater than the second-

   var compareNames = function(a, b) {    
    var nameA = a.awd.toLowerCase();
    var nameB = b.awd.toLowerCase();
    if(nameA==nameB) return 0;
    return (nameA > nameB)? 1:-1; 
};

1 Comment

I still get the same output when I use the above: var tempList=content.awds.sort(compareNames); for (var i= 0,lgh=tempList.length;i<lgh;i++){ console.log('awd4 = ' + tempList[i].awd ); }

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.