Titanium Community Questions & Answer Archive

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

Array sort for associative arrays not working

I am trying to sort an array I will add to a TableView. I use the following sort method, which works fine in Firebug console, yet when I run it within Titanium, it has zero effect.

I assume this might be an internal bug with how the sort method is coded within Titanium Mobile, but hopefully I am missing something.

I'm using Snow Leopard 10.6.4 and emulating on iOS 4.1. If you need other environment info let me know and I'd be happy to provide.

var data_remote = [
    {id: 283, title: 'Last'},
    {id: 498, title: 'First'},
    {id: 3824, title: 'Me'},
    {id: 938, title: 'Zebra'},
    {id: 4938, title: 'Rocker'},
    {id: 4382, title: 'Tanzina'},
    {id: 583, title: 'Jones'},
    {id: 28, title: 'Sharks'}
];

// Show the default data prior to sorting
for (var x = 0; x < data_remote.length; x++) {
    Ti.API.info('before: id : ' + data_remote[x].id + ', title : ' + data_remote[x].title);
}

// Sorting by Title
data_remote = data_remote.sort( function (a,b) { return a['title'] > b['title']; } );

// Show the data soft order after sorting 
for (var z = 0; z < data_remote.length; z++) {
    Ti.API.info('after: id : ' + data_remote[z].id + ', title : ' + data_remote[z].title);
}
— asked November 2nd 2010 by Nathaniel Brown
  • array
  • ios
  • iphone
  • leopard
  • osx
  • snow
  • sort
  • tableview
0 Comments

3 Answers

  • This seems to work.

    function sortByNestedTitle(a, b) {
        var x = a.title.toLowerCase();
        var y = b.title.toLowerCase();
        return ((x < y) ? -1 : ((x > y) ? 1 : 0));
    }
    
    data_remote.sort(sortByNestedTitle);
    
    — answered November 2nd 2010 by Nathaniel Brown
    permalink
    0 Comments
  • You beat me to it, I was about to post code that was similar to that. Yes, that way should work well for what you are doing.

    — answered November 2nd 2010 by John McKnight
    permalink
    0 Comments
  • I have a requirement to do a sort of an array on iPHONE and ANDROID. But the array is around 500 rows. THe sort works on an emulator with 10 rows. But seems to hang on the larger 500 rows. And more than likely this will be a 1000 row sort someday. So, this is the code. Perhaps someone could suggestion a better solution?

    //sort the rows array
    function sortByLastName(a,b){
        var aa = a.ln.toLowerCase();
        var bb = b.ln.toLowerCase();
    
    Ti.API.info('inside the sort array');
    
    
    Ti.API.info('a.lastname - b.lastName-----   ' + a.ln + ' === '+ b.ln);
    
    if (aa < bb) //sort string ascending
            {return -1;};
    if (aa > bb)
            {return 1;};
    
    };
    
    — answered December 29th 2010 by vincent youmans
    permalink
    0 Comments
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.