Thursday, August 15, 2013

Sorting the Data with String,Integers and Decimals using Javascript

Sorting the Data with String,Integers and Decimals using Javascript
In this article,i would like to explain "How to sort data with String,Integers and Decimals values using javascript".In have already articles related javascript. For example, Get unique values from json using jquery , Simple Way to create Timer using Javascript.Here i am sharing article related to sorting technique using javascript. 



facebook_login_button
Main Logic for Sorting: 

Sorting with String :

function SortByName(x, y) {     
        return ((x.winename == y.winename) ? 0 : ((x.winename > y.winename) ? 1 : -1));
    }

Sorting with Integers :
function SortByPrice(x, y) { 
    return parseFloat(y.price) - (x.price);
    }

Sorting with Decimals:
function SortByRating(x, y) {
    return ((y.reviewstars == x.reviewstars) ? 0 : ((y.reviewstars > x.reviewstars) ? 1 : -1));
    }

Complete Logic : 

Following is the code to bind the data to listview based sorting requested from json.
$('#BindWines').live('pageshow', function() {
$('#completeWines li').remove();
    jQuery.getJSON("Js/newwinecurrent.json", function(data) {
        
        wines = data.rows;
        if (query_string[0] == "Price") {
            wines.sort(SortByPrice);
        }
        else if (query_string[0] == "Rating") {
            wines.sort(SortByRating);
        }
        else {
            wines.sort(SortByName);
        }
        $.each(wines, function(index, wine) {
            if (parseInt(wine.price) >= parseInt(subpar1) && wine.reviewstars >= subpar2) {                
                $('#completeWines').append('
  • ID:' + wine.uniqueid + '. ' + 'Price :' + wine.price + ' ' + 'Review Stars :' + wine.reviewstars + ' ' + 'Review Text:' + wine.reviewtext + ' ' + '
  • '); } $('#completeWines').listview('refresh'); }); }); });
    That's it. If you any queries.Please drop a message.

    Labels: , , ,

    2 Comments:

    At August 16, 2013 at 9:21 AM , Anonymous Anonymous said...

    Hello There. I found your blog using msn. This is an extremely well written article.
    I'll be sure to bookmark it and come back to read more of your useful information. Thanks for the post. I will definitely comeback.

    Also visit my weblog - more info - www.myhack24.com -

     
    At August 26, 2013 at 7:03 PM , Anonymous Los Angeles Magento developer said...

    Great tutorial. This will be very helpful for the developers. One of my friend is looking for the same kind of solution and I am going to provide this link to him. Thanks for sharing this here with us.

     

    Post a Comment

    Subscribe to Post Comments [Atom]

    << Home