The Little Guy Posted April 1, 2012 Share Posted April 1, 2012 What is the best way to compare two strings and alphabetize them? if(string >= string2){ $("#"+currentTestID).before(html); } But that doesn't seem to always work. I had these three strings: - Chrysanthemum.jpg - Cindy.jpg - Jaimee.jpg I was then trying to decide where to place this string: - Family.jpg The code above decided it goes best between the two strings That start with "C", when really it should go after "Cindy.jpg". So, Am I comparing strings wrong for alphabetizing? Quote Link to comment https://forums.phpfreaks.com/topic/260142-alphabetical-order/ Share on other sites More sharing options...
Adam Posted April 1, 2012 Share Posted April 1, 2012 What are the values of string and string2? My guess would be that your sorting logic is a little off - how are you doing it? Quote Link to comment https://forums.phpfreaks.com/topic/260142-alphabetical-order/#findComment-1333313 Share on other sites More sharing options...
The Little Guy Posted April 1, 2012 Author Share Posted April 1, 2012 string is one of the following (loop through them): Chrysanthemum.jpg Cindy.jpg Jaimee.jpg string2 is: Family.jpg here is the actual code I am using if that helps: $("td.basename").each(function(){ var itmid = $(this).parent().attr("id"); var type = $(this).parent().children(".type").text().toLowerCase(); if($(this).text() >= newName && !added && type != "folder"){ $("#"+itmid).before("<tr class='item' id='item_"+openFile+"'>"+rowData+"</tr>"); added = true; return; } }); Quote Link to comment https://forums.phpfreaks.com/topic/260142-alphabetical-order/#findComment-1333316 Share on other sites More sharing options...
Adam Posted April 1, 2012 Share Posted April 1, 2012 Where is newName defined? Quote Link to comment https://forums.phpfreaks.com/topic/260142-alphabetical-order/#findComment-1333326 Share on other sites More sharing options...
The Little Guy Posted April 1, 2012 Author Share Posted April 1, 2012 above the each as: var newName = $("#file-input-name").val(); // From an input field Quote Link to comment https://forums.phpfreaks.com/topic/260142-alphabetical-order/#findComment-1333332 Share on other sites More sharing options...
Adam Posted April 2, 2012 Share Posted April 2, 2012 Put together this little example, which is the same concept as yours but works exactly as it should. The problem is with your implementation, but you're only providing abstract pieces of it so it's hard to build up a decent picture of what's happening. Quote Link to comment https://forums.phpfreaks.com/topic/260142-alphabetical-order/#findComment-1333525 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.