Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

return contacts array by lastName

var people = Titanium.Contacts.getAllPeople();… is there a way to return the array sorted by lastName?

— asked November 22nd 2010 by vincent youmans
  • contacts

2 Answers

  • I looked through the code and it seem you can only sort when you are retrieving users from a group.

    if there is someone out there who knows differently, please let me know

    — answered November 22nd 2010 by Aaron Saunders
    1 Comment
    • I see that as well, but I still don't understand all of the parameters to call by group. Or how the group works.
      I can not even find the KS example.

      — commented November 23rd 2010 by vincent youmans
  • Titanium.Contacts is a crock. It will crash your users' devices.

    This is the only way I have been able to get a sorted name list:

    var contacts = Titanium.Contacts.getAllPeople();
    function sortByLastName(a, b) {
        var x = JSON.stringify(a.lastName).toUpperCase();
        var y = JSON.stringify(b.lastName).toUpperCase(); 
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));


    — answered November 23rd 2010 by John Holman
    • That is exactly what I have been doing. but there are two problems with it.
      1… On large Contact dayabases ( >200 in my situation ), the sort never ends. I think I am running out of RAM. On small contact DB's its slow but eventually finishes.
      2… The .toUpperCase() does not seem to run on ANDROID.

      But I have a question about your code. I see JSON.stringify(b.lastName).toUpperCase();
      Where is the JSON coming from?

      — commented November 23rd 2010 by vincent youmans
    • I can't get it to work on iPad (3.2) with just a.lastName.toUppercase()
      I assumed that's because it is some sort of Object and not just a text string.
      Whatever the reason, the above works for me.
      regards, JH.

      — commented November 23rd 2010 by John Holman
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.