0

I want to sort my array by lastName attribute. The array stays exactly the same. Here is the code:

console.log(clientListArray);
    //sort clientList by last name
    var sortedtClientListArray = clientListArray.sort(function(obj1, obj2) {
        console.log(obj1.lastName);
        return obj1.lastName - obj2.lastName;
    })
    console.log(sortedtClientListArray);

A console.log of my array that stays the same (diving in to the 4th element (index 3)):

0: ct.extend.init
1: ct.extend.init
2: ct.extend.init
3: ct.extend.init
_events: Object
dwelling: "RH07"
firstName: "Alan"
lastName: "Mosby"
letter: "M"
nhi: ""
oid: "2143.10"
parent: function (){return i}
uid: "79fbbf40-5545-4cdc-bc2b-088bf56affc6"
__proto__: r
4: ct.extend.init
5: ct.extend.init
6: ct.extend.init
7: ct.extend.init
length: 8
__proto__: Array[0]

Why is the order of objects in the array not changing?

The full method that it is inside:

function onClientClick(e) {


    console.log(e.dataItem);
    var clientList = e.sender.dataSource._data;
    console.log(clientList);
    var clientListArray = [];
    for(i=0; i < clientList.length; i++){
        clientListArray.push(clientList[i]);
    }
    console.log(clientListArray);
    //sort clientList by last name
    var sortedtClientListArray = clientListArray.sort(function(obj1, obj2) {
        console.log(obj1.lastName);
        return obj1.lastName - obj2.lastName;
    })
    console.log(sortedtClientListArray);

    for(i=0; i < clientList.length; i++){
        var clientID = clientList[i].oid;
        if(clientID == e.dataItem.oid) {
            theClient = new Client(clientList[i]);
            console.log(i);
            console.log(theClient.name);
            navigateToSingleClient(true, clientList[i], true, clientList, i);
        }
    }


}
4
  • 2
    can you post the content of array. Commented Aug 20, 2014 at 4:19
  • @Mritunjay Have I not done that? The console.log of the array? Commented Aug 20, 2014 at 4:24
  • Nope, we can only see the attributes of one item. Commented Aug 20, 2014 at 4:25
  • got the problem added the answer. Commented Aug 20, 2014 at 4:29

1 Answer 1

1

There are two problems in your code.

1) "OneString"-"SecondString" will return NaN. So ca return that in this situation.

2) Array.sort sorts the given array in place. It returns the sorted array.

Say like this

clientListArray.sort(function(a, b){
    if(a.lastName < b.lastName) return -1;
    if(a.lastName > b.lastName) return 1;
    return 0;
})
Sign up to request clarification or add additional context in comments.

3 Comments

According to MDN "The sort() method sorts the elements of an array in place and returns the array." so it does return the array.
But you say "it doesn't return an array." but MDN says it does return an array.
@AndrewHarvey My Bad, Updated the answer. Though I am not sure why would you sort the array in place and then return the sorted array back.

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.