0

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 ?

4
  • 1
    Can you create a jsfiddle to show the problem? Commented Feb 23, 2015 at 15:26
  • 1
    here you get the url of the page.. If you click on the nav button the chart in the bottom shold show the values sorted! And this work only on the last button link Commented Feb 23, 2015 at 15:29
  • WeedMap.de! right ok Commented Feb 23, 2015 at 15:30
  • 1
    you will see it only works well in firefox Commented Feb 23, 2015 at 15:39

1 Answer 1

1

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();

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.