Manuel
Manuel

Reputation: 2542

Issue to sort array in webkit browsers

I got an issue while sorting arrays in webkit browsers. In firefox the sorting works very well but in chrome or safari it sorts only my last if() statement. For example in this code it sorts only this block: (The if conditions are tested and working)

if(printCategory == printCategoryTour)
    {
        function compare (a, b) {

        return b.tour_naechtigungen - a.tour_naechtigungen

    };
        chartDataRegions.sort(compare);
        pushValues();
    }

on this function:

function chartDataSortArray() 

{       

    arraySorted = [];

    if(printCategory == printCategoryBev)
    {
        function compare (a, b) {

        return b.bev - a.bev;

        };
        chartDataRegions.sort(compare);

        pushValues();
    }
    if(printCategory == printCategoryWirt)
    {
        function compare (a, b) {

        return b.wirt_unt_anzahl - a.wirt_unt_anzahl

        };
        chartDataRegions.sort(compare);
        pushValues();           
    }
    if(printCategory == printCategoryMob)
    {
        function compare (a, b) {

        return b.mob_einpendler - a.mob_einpendler 

        };
        chartDataRegions.sort(compare);
        pushValues();
    }
    if(printCategory == printCategoryTour)
    {
        function compare (a, b) {

        return b.tour_naechtigungen - a.tour_naechtigungen

        };
        chartDataRegions.sort(compare);
        pushValues();
    }   

function pushValues()
{   
    for(i = 0; i<chartDataRegions.length; i++)
{
    arraySorted.push(chartDataRegions[i])

}
}

Hmm i tried it now 2 hours but i dont get it to work ! Does anybody find the issue ?

Upvotes: 0

Views: 214

Answers (1)

selman
selman

Reputation: 168

Set the compare function once ! It looks better and works crossbrowser

function chartDataSortArray() 

{       

        arraySorted = [];

            function compare (a, b) {
                if(printCategory == printCategoryBev)
                    {
                        return b.bev - a.bev;
                    }
                if(printCategory == printCategoryWirt)
                    {
                        return b.wirt_unt_anzahl - a.wirt_unt_anzahl;
                    }
                if(printCategory == printCategoryMob)
                    {
                        return b.mob_einpendler - a.mob_einpendler
                    }
                if(printCategory == printCategoryTour)
                    {
                        return b.tour_naechtigungen - a.tour_naechtigungen
                    }
                };
            function pushValues()
            {   
                for(i = 0; i<chartDataRegions.length; i++)
                    {
                        arraySorted.push(chartDataRegions[i])
                    }
            }


    chartDataRegions.sort(compare);

    pushValues();

}

Upvotes: 1

Related Questions