00robinette
00robinette

Reputation: 567

Sort getJson using pure JS - no plugins

So I am trying to sort an external json file. The end goal will be to output this data to a table and allow the user to click on the table header and sort the data. I am first just trying to figure out how to sort the data. I am close as the function below works in 2 out of 3 cases. I have to use pure JS and have the following code:

<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta http-equiv="content-type" content="text/html;charset=Windows-1252">

    </head>
    <body>
        <div id="catTable"></div>
        <script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
        <script>


    function sortJsonArrayByProperty(objArray, prop, direction){
        if (arguments.length<2) throw new Error("sortJsonArrayByProp requires 2 arguments");
        var direct = arguments.length>2 ? arguments[2] : 1; //Default to ascending

        if (objArray && objArray.constructor===Array){
            var propPath = (prop.constructor===Array) ? prop : prop.split(".");
            objArray.sort(function(a,b){
                for (var p in propPath){
                    if (a[propPath[p]] && b[propPath[p]]){
                        a = a[propPath[p]];
                        b = b[propPath[p]];
                    }
                }
                // convert numeric strings to integers
                a = a.match(/^\d+$/) ? +a : a;
                b = b.match(/^\d+$/) ? +b : b;
                return ( (a < b) ? -1*direct : ((a > b) ? 1*direct : 0) );
            });
        }
    }


     $.getJSON('cats.json', function(cats) {
        sortJsonArrayByProperty(cats, 'cats.coffeePreference');

        document.write(cats[0].country);
        document.write(cats[0].breed);
        document.write(cats[0].coffeePreference);  
        document.write(cats[1].country);
        document.write(cats[1].breed);
        document.write(cats[1].coffeePreference);
      });

    </script>
    </body>
    </html>

and the following external json file cats.json

[{
                    "breed" : "Abyssinian",
                    "country" : "Ethiopia",
                    "coffeePreference" : "espresso",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Gustav_chocolate.jpg/100px-Gustav_chocolate.jpg"
                }, {
                    "breed" : "Aegean",
                    "country" : "Greece",
                    "coffeePreference" : "medium roast, cream and sugar",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Aegean_cat.jpg/100px-Aegean_cat.jpg"
                }, {
                    "breed" : "American Curl",
                    "country" : "United States",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/American_curl_2.jpg/100px-American_curl_2.jpg"
                }, {
                    "breed" : "American Bobtail",
                    "country" : "United States",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/88/American_bobtail_2.jpg/100px-American_bobtail_2.jpg"
                }, {
                    "breed" : "American Shorthair",
                    "country" : "United States",
                    "coffeePreference" : "Skinny Mocha Latte",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/American_Shorthair.jpg/100px-American_Shorthair.jpg"
                }, {
                    "breed" : "American Wirehair",
                    "country" : "United States",
                    "coffeePreference" : "Skinny Mocha Latte",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/American_Wirehair…JPG/100px-American_Wirehair_-_CFF_cat_show_Heinola_2008-05-04_IMG_8721.JPG"
                }, {
                    "breed" : "Arabian Mau",
                    "country" : "Arabian Peninsula",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/3yoArabianMau.jpeg/100px-3yoArabianMau.jpeg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Australian Mist",
                    "country" : "Australia",
                    "coffeePreference" : "Irish Coffee",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Australian_Mist.jpg/100px-Australian_Mist.jpg"
                }, {
                    "breed" : "Asian",
                    "country" : "developed in the United Kingdom (founding stock from Asia)",
                    "coffeePreference" : "Iced Coffee",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/BrownVarientAsianCat.JPG/100px-BrownVarientAsianCat.JPG"
                }, {
                    "breed" : "Asian Semi-longhair",
                    "country" : "United Kingdom",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Tiffanie_at_cat_show.jpg/100px-Tiffanie_at_cat_show.jpg"
                }, {
                    "breed" : "Balinese",
                    "country" : "developed in the United States (founding stock from Thailand)",
                    "coffeePreference" : "French Roast",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Oskar.jpg/100px-Oskar.jpg"
                }, {
                    "breed" : "Bambino",
                    "country" : "United States",
                    "picture" : "",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Bengal",
                    "country" : "developed in the United States (founding stock from Asia)",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/BengalCat_Stella.jpg/100px-BengalCat_Stella.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Birman",
                    "country" : "Burma",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Birman2.jpg/100px-Birman2.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Bombay",
                    "country" : "developed in the United States (founding stock from Asia)",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/16/Bombay_cat.jpg/100px-Bombay_cat.jpg"
                }, {
                    "breed" : "Brazilian Shorthair",
                    "country" : "Brazil",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Gato_pelo_curto_brasileiro.JPG/100px-Gato_pelo_curto_brasileiro.JPG"
                }, {
                    "breed" : "British Semi-longhair",
                    "country" : "United Kingdom",
                    "coffeePreference" : "Americano"
                }, {
                    "breed" : "British Shorthair",
                    "country" : "United Kingdom",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Britishblue.jpg/100px-Britishblue.jpg"
                }, {
                    "breed" : "British Longhair",
                    "country" : "United Kingdom",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/British_Longhair_-_Black_Silver_Shaded.jpg/100px-British_Longhair_-_Black_Silver_Shaded.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Burmese",
                    "country" : "Burma and Thailand",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/Blissandlucky11.jpg/100px-Blissandlucky11.jpg"
                }, {
                    "breed" : "Burmilla",
                    "country" : "United Kingdom",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Male_Burmilla_cat.jpeg/100px-Male_Burmilla_cat.jpeg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "California Spangled",
                    "country" : "United States",
                    "coffeePreference" : "Cream, No Sugar",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6c/Star_Spangled_Cat.jpg/100px-Star_Spangled_Cat.jpg"
                }, {
                    "breed" : "Chantilly-Tiffany",
                    "country" : "United States",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Tiffany3.jpg/100px-Tiffany3.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Chartreux",
                    "country" : "France",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Abbaye_fev2006_003.jpg/100px-Abbaye_fev2006_003.jpg"
                }, {
                    "breed" : "Chausie",
                    "country" : "France",
                    "coffeePreference" : "Two Sugars",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/Chausiecatexample.jpg/100px-Chausiecatexample.jpg"
                }, {
                    "breed" : "Cheetoh",
                    "country" : "United States",
                    "coffeePreference" : "Cream, No Sugar",
                    "picture" : "https://upload.wikimedia.org/wikipedia/en/thumb/6/6a/Cheetoh_cat_chilling.png/100px-Cheetoh_cat_chilling.png"
                }, {
                    "breed" : "Colorpoint Shorthair",
                    "country" : "",
                    "picture" : "https://upload.wikimedia.org/wikipedia/en/thumb/d/d5/Jake117.jpg/100px-Jake117.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Cornish Rex",
                    "country" : "United Kingdom (England)",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/BebopsLilacPrince.JPG/100px-BebopsLilacPrince.JPG"
                }, {
                    "breed" : "Cymric or Manx Longhair",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Cymric_-_Norwegia…ric_-_Norwegian_forest_cat_presentation_show_Kotka_2009-02-01_IMG_0687.JPG",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Cyprus",
                    "country" : "Cyprus",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b9/CyprusShorthair.jpg/100px-CyprusShorthair.jpg"
                }, {
                    "breed" : "Devon Rex",
                    "country" : "United Kingdom (England)",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Devon_Rex_Izzy.jpg/100px-Devon_Rex_Izzy.jpg"
                }, {
                    "breed" : "Donskoy or Don Sphynx",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Cat_don_sphinx.JPG/100px-Cat_don_sphinx.JPG",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Dragon Li",
                    "country" : "China",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/Dragon_Li_-_Li_Hua_Mau1.jpg/100px-Dragon_Li_-_Li_Hua_Mau1.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Dwarf cat or Dwelf",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Egyptian Mau",
                    "country" : "Italy",
                    "coffeePreference" : "Cream, No Sugar",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Egy_mau.jpg/100px-Egy_mau.jpg"
                }, {
                    "breed" : "European Shorthair",
                    "country" : "Sweden",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/European_shorthair_procumbent_Quincy.jpg/100px-European_shorthair_procumbent_Quincy.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Exotic Shorthair",
                    "country" : "United States",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Cream_tabby_exotic_cat.jpg/100px-Cream_tabby_exotic_cat.jpg"
                }, {
                    "breed" : "German Rex",
                    "country" : "East Germany",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/German_Rex_Emi.jpg/100px-German_Rex_Emi.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Havana Brown",
                    "country" : "United Kingdom",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Havana_Brown.jpg/100px-Havana_Brown.jpg"
                }, {
                    "breed" : "Highlander",
                    "country" : "United Kingdom (Scotland)",
                    "coffeePreference" : "Americano"
                }, {
                    "breed" : "Himalayan or Colorpoint Persian",
                    "coffeePreference" : "French Roast",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dc/Chocolate_Himlayan.jpg/100px-Chocolate_Himlayan.jpg"
                }, {
                    "breed" : "Japanese Bobtail",
                    "country" : "Japan",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/JapaneseBobtailBlueEyedMi-ke.JPG/100px-JapaneseBobtailBlueEyedMi-ke.JPG",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Javanese",
                    "country" : "United States",
                    "coffeePreference" : "French Roast",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Javanese_cat.jpg/100px-Javanese_cat.jpg"
                }, {
                    "breed" : "Kurilian Bobtail",
                    "country" : "Western Russia",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Khao Manee",
                    "country" : "Thailand",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Khao_Manee_%22ChaWee%22.jpg/100px-Khao_Manee_%22ChaWee%22.jpg"
                }, {
                    "breed" : "Korat",
                    "country" : "Thailand",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Veda%2Cchat-adult…m%C3%A2le-race-korat.JPG/100px-Veda%2Cchat-adulte-m%C3%A2le-race-korat.JPG"
                }, {
                    "breed" : "Korn Ja",
                    "country" : "Thailand",
                    "coffeePreference" : "Carmel Latte Grande"
                }, {
                    "breed" : "Kurilian Bobtail or Kuril Islands Bobtail",
                    "country" : "Japan ",
                    "picture" : " https : //upload.wikimedia.org/wikipedia/commons/thumb/8/82/Kurilian_bobtail.JPG/100px-Kurilian_bobtail.JPG",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "LaPerm",
                    "country" : "United States",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Laperm_LH_red_tabby.jpg/100px-Laperm_LH_red_tabby.jpg"
                }, {
                    "breed" : "Lykoi",
                    "country" : "United States",
                    "coffeePreference" : "Two Sugars"
                }, {
                    "breed" : "Maine Coon",
                    "country" : "United States",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/Maine_Coon_female.jpg/100px-Maine_Coon_female.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Manx",
                    "country" : "United Kingdom (Isle of Man)",
                    "coffeePreference" : "Skinny Mocha Latte",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Japanese_Bobtail_looking_like_Manx.jpg/100px-Japanese_Bobtail_looking_like_Manx.jpg"
                }, {
                    "breed" : "Mekong Bobtail",
                    "country" : "Russia",
                    "coffeePreference" : "French Roast",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Mekong_bobtail_fe…_Pride_cattery.jpg/100px-Mekong_bobtail_female%2C_Cofein_Pride_cattery.jpg"
                }, {
                    "breed" : "Minskin",
                    "country" : "United States",
                    "coffeePreference" : "Americano",
                    "picture" : ""
                }, {
                    "breed" : "Munchkin",
                    "country" : "United States",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Longhairedmunchkin.jpg/100px-Longhairedmunchkin.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Nebelung",
                    "country" : "United States",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Nebelung_Male%2C_…n_Song_de_Chine.JPG/100px-Nebelung_Male%2C_Aleksandr_van_Song_de_Chine.JPG"
                }, {
                    "breed" : "Norwegian Forest Cat",
                    "country" : "Norway",
                    "coffeePreference" : "Skinny Mocha Latte",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Norskskogkatt_Evita_3.JPG/100px-Norskskogkatt_Evita_3.JPG"
                }, {
                    "breed" : "Ocicat",
                    "country" : "United States",
                    "coffeePreference" : "Cream, No Sugar",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Ocicat-Charan.jpg/100px-Ocicat-Charan.jpg"
                }, {
                    "breed" : "Oriental Bicolor",
                    "country" : "",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6a/Oriental_shorthair_20070130_caroline.jpg/100px-Oriental_shorthair_20070130_caroline.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Oriental Shorthair",
                    "country" : "",
                    "coffeePreference" : "Skinny Mocha Latte",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Oriental_Shorthai…ile%29.jpg/100px-Oriental_Shorthair_Blue_Eyed_White_cat_%28juvenile%29.jpg"
                }, {
                    "breed" : "Oriental Longhair",
                    "country" : "",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/OLH-GIP_Divan_Cesar.jpg/100px-OLH-GIP_Divan_Cesar.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Persian (Modern Persian Cat)",
                    "country" : "United States",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Persialainen.jpg/100px-Persialainen.jpg"
                }, {
                    "breed" : "Persian (Traditional Persian Cat)",
                    "country" : "Greater Iran",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/SnowyandHazy.jpg/100px-SnowyandHazy.jpg"
                }, {
                    "breed" : "Peterbald",
                    "country" : "Russia",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b7/Tamila_the_lilac_tabby_Peterbald_cat.jpg/100px-Tamila_the_lilac_tabby_Peterbald_cat.jpg"
                }, {
                    "breed" : "Pixie-bob",
                    "country" : "United States",
                    "coffeePreference" : "Cream, No Sugar",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Jarnac_Bepacific_feb07.jpg/100px-Jarnac_Bepacific_feb07.jpg"
                }, {
                    "breed" : "Ragamuffin",
                    "country" : "United States",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/20050922AmarilloRes.jpg/100px-20050922AmarilloRes.jpg"
                }, {
                    "breed" : "Ragdoll",
                    "country" : "United States",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Ragdoll_from_Gatil_Ragbelas.jpg/100px-Ragdoll_from_Gatil_Ragbelas.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Russian Blue",
                    "country" : "Russia",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Russian_Blue_001.gif/100px-Russian_Blue_001.gif"
                }, {
                    "breed" : "Russian White, Black and Tabby",
                    "country" : "Australia",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Sam Sawet",
                    "country" : "Thailand",
                    "coffeePreference" : "Carmel Latte Grande"
                }, {
                    "breed" : "Savannah",
                    "country" : "United States",
                    "coffeePreference" : "Cream, No Sugar",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/Savannah_Cat_portrait.jpg/100px-Savannah_Cat_portrait.jpg"
                }, {
                    "breed" : "Scottish Fold",
                    "country" : "United Kingdom (Scotland)",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Lilac_Scottish_Fold.jpg/100px-Lilac_Scottish_Fold.jpg"
                }, {
                    "breed" : "Selkirk Rex",
                    "country" : "United States",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/PolloSelkirkRex.jpg/100px-PolloSelkirkRex.jpg"
                }, {
                    "breed" : "Serengeti",
                    "country" : "United States",
                    "coffeePreference" : "Cream, No Sugar",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Serengetimalecat.jpg/100px-Serengetimalecat.jpg"
                }, {
                    "breed" : "Serrade petit",
                    "country" : "France",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Serrade_petit.jpg/100px-Serrade_petit.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Siamese",
                    "country" : "Thailand",
                    "coffeePreference" : "French Roast",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Siam_lilacpoint.jpg/100px-Siam_lilacpoint.jpg"
                }, {
                    "breed" : "Siberian",
                    "country" : "Russia",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Qgcfillimor.jpg/100px-Qgcfillimor.jpg"
                }, {
                    "breed" : "Singapura",
                    "country" : "Singapore",
                    "coffeePreference" : "Two Sugars",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Raffles_singapura_cat.jpg/100px-Raffles_singapura_cat.jpg"
                }, {
                    "breed" : "Snowshoe",
                    "country" : "United States",
                    "coffeePreference" : "French Roast",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Snowshoe_%28cat%29.JPG/100px-Snowshoe_%28cat%29.JPG"
                }, {
                    "breed" : "Sokoke",
                    "country" : "Kenya",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Sokoke_dalili.jpg/100px-Sokoke_dalili.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Somali",
                    "country" : "Somalia",
                    "coffeePreference" : "Two Sugars",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c6/Blue_Somali_kitten_age_3_months.jpg/100px-Blue_Somali_kitten_age_3_months.jpg"
                }, {
                    "breed" : "Sphynx",
                    "country" : "Canada",
                    "coffeePreference" : "Americano",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Sphinx2_July_2006.jpg/100px-Sphinx2_July_2006.jpg"
                }, {
                    "breed" : "Suphalak",
                    "country" : "Thailand",
                    "coffeePreference" : "Carmel Latte Grande",
                    "picture" : "https://upload.wikimedia.org/wikipedia/en/thumb/5/52/Suphalak_Female_in_Tha…umDaengManee.jpg/100px-Suphalak_Female_in_Thailand_named_AumDaengManee.jpg"
                }, {
                    "breed" : "Thai",
                    "country" : "Thailand",
                    "coffeePreference" : "French Roast",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/Mimbi3.JPG/100px-Mimbi3.JPG"
                }, {
                    "breed" : "Tonkinese",
                    "country" : "Canada",
                    "coffeePreference" : "Turkish",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Tonkinese.gif/100px-Tonkinese.gif"
                }, {
                    "breed" : "Toyger",
                    "country" : "United States",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Toyger_-_Cornish_…ger_-_Cornish_Rex_presentation_show_Riihim%C3%A4ki_2008-11-16_IMG_0101.JPG",
                    "coffeePreference" : "Cafe Au Lait"
                }, {
                    "breed" : "Turkish Angora",
                    "country" : "Turkey",
                    "coffeePreference" : "Skinny Mocha Latte",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Angora.jpg/100px-Angora.jpg"
                }, {
                    "breed" : "Turkish Van",
                    "country" : "developed in the United Kingdom (founding stock from Turkey)",
                    "picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Turkish_Van_Example2.jpg/100px-Turkish_Van_Example2.jpg",
                    "coffeePreference" : "Cafe Au Lait"
                }
            ]

The sort function works properly when called on cats.breed andcats.coffeePreference but I get a blank screen when calling the function on cats.country. Can anyone explain why this is happening?

Upvotes: 1

Views: 103

Answers (1)

Carsten Massmann
Carsten Massmann

Reputation: 28226

Latest update

When you sort your recently posted larger json-object your sort function will incurr the following error (visible in your developer console window): "Exception: TypeError: a.match is not a function".

This is caused by your attempt to test for numeric contents. .match() is a method of the String object, so when a.country or b.country happens to be empty (=undefined) then there is no string object where the function match() could be found! If you comment out the concerning lines

 //  a = a.match(/^\d+$/) ? +a : a;
 //  b = b.match(/^\d+$/) ? +b : b;

(or use the sort function I suggested, see my edited version below) your problem is gone.

Update

I had a closer look at your own sorting routine and found the following:

  1. The second argument prop is definitely too complicated: You enter a composite object name there 'cats.coffeePreference' which is then split into its two components. So far so good, but then you loop over the components (in your example 'cats' and 'coffeePreference') and expect to find properties with these names in your cats-objects a and b passed to the sort-callback function. The first property 'cats' will always be undefined, so it's basically a waste of time (and code) looking for it. Luckily you check for existance first, but nonetheless it is superfluous.

  2. The check for numerical data is basically OK, but half-hearted. It only looks for strings consisting of numbers and nothing else. So, if a variable contained a string like ' 1' or '2.5' (or stuff like '-.1' or '10.30 ') would not be recognised as being "numeric". A Regexp-based test like

    /^(?=.*\d)\s*-?\d*\.?\d*\s*?$/.test(a);
    

    or alternatively the much simpler function

    !isNaN(a)
    

    will tell you whether a is numeric or not.

I tested your sort function and, despite the above mentioned shortcomings: it still works!


However, here is my somewhat simplified version of your sort:

function catsrt(arr,prop,dir){
 dir=dir||1;                            // default sort direction
 arr.sort(function(a,b){  
   var aa=a[prop]||'0',bb=b[prop]||'0'; // '0' in case of "undefined" entries
   return aa.localeCompare(bb)*dir;     // use string method localeCompare
 });
}
$('button').click(function(){
  var prop=$(this).text(),dir=$('input:checked').length==1?-1:1;
  catsrt(cats,prop,dir);
  $('#out').html($.map(cats,function(cat){return $.map(cat,function(v,k){
    return k+':<span class="'+k+'">'+v+'</span>';}).join(',');}).join('<hr>'));
  $('.'+prop).css('color',dir>0?'green':'red');
});

var cats=[{
"breed" : "Abyssinian",
"country" : "Ethiopia",
"coffeePreference" : "espresso",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9b/Gustav_chocolate.jpg/100px-Gustav_chocolate.jpg"
}, {
"breed" : "Aegean",
"country" : "Greece",
"coffeePreference" : "medium roast, cream and sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/51/Aegean_cat.jpg/100px-Aegean_cat.jpg"
}, {
"breed" : "American Curl",
"country" : "United States",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/17/American_curl_2.jpg/100px-American_curl_2.jpg"
}, {
"breed" : "American Bobtail",
"country" : "United States",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/88/American_bobtail_2.jpg/100px-American_bobtail_2.jpg"
}, {
"breed" : "American Shorthair",
"country" : "United States",
"coffeePreference" : "Skinny Mocha Latte",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0c/American_Shorthair.jpg/100px-American_Shorthair.jpg"
}, {
"breed" : "American Wirehair",
"country" : "United States",
"coffeePreference" : "Skinny Mocha Latte",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/American_Wirehair…JPG/100px-American_Wirehair_-_CFF_cat_show_Heinola_2008-05-04_IMG_8721.JPG"
}, {
"breed" : "Arabian Mau",
"country" : "Arabian Peninsula",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/53/3yoArabianMau.jpeg/100px-3yoArabianMau.jpeg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Australian Mist",
"country" : "Australia",
"coffeePreference" : "Irish Coffee",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f0/Australian_Mist.jpg/100px-Australian_Mist.jpg"
}, {
"breed" : "Asian",
"country" : "developed in the United Kingdom (founding stock from Asia)",
"coffeePreference" : "Iced Coffee",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/BrownVarientAsianCat.JPG/100px-BrownVarientAsianCat.JPG"
}, {
"breed" : "Asian Semi-longhair",
"country" : "United Kingdom",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/a/a7/Tiffanie_at_cat_show.jpg/100px-Tiffanie_at_cat_show.jpg"
}, {
"breed" : "Balinese",
"country" : "developed in the United States (founding stock from Thailand)",
"coffeePreference" : "French Roast",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e6/Oskar.jpg/100px-Oskar.jpg"
}, {
"breed" : "Bambino",
"country" : "United States",
"picture" : "",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Bengal",
"country" : "developed in the United States (founding stock from Asia)",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9f/BengalCat_Stella.jpg/100px-BengalCat_Stella.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Birman",
"country" : "Burma",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Birman2.jpg/100px-Birman2.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Bombay",
"country" : "developed in the United States (founding stock from Asia)",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/16/Bombay_cat.jpg/100px-Bombay_cat.jpg"
}, {
"breed" : "Brazilian Shorthair",
"country" : "Brazil",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/Gato_pelo_curto_brasileiro.JPG/100px-Gato_pelo_curto_brasileiro.JPG"
}, {
"breed" : "British Semi-longhair",
"country" : "United Kingdom",
"coffeePreference" : "Americano"
}, {
"breed" : "British Shorthair",
"country" : "United Kingdom",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9d/Britishblue.jpg/100px-Britishblue.jpg"
}, {
"breed" : "British Longhair",
"country" : "United Kingdom",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/50/British_Longhair_-_Black_Silver_Shaded.jpg/100px-British_Longhair_-_Black_Silver_Shaded.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Burmese",
"country" : "Burma and Thailand",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/0/04/Blissandlucky11.jpg/100px-Blissandlucky11.jpg"
}, {
"breed" : "Burmilla",
"country" : "United Kingdom",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e9/Male_Burmilla_cat.jpeg/100px-Male_Burmilla_cat.jpeg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "California Spangled",
"country" : "United States",
"coffeePreference" : "Cream, No Sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6c/Star_Spangled_Cat.jpg/100px-Star_Spangled_Cat.jpg"
}, {
"breed" : "Chantilly-Tiffany",
"country" : "United States",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Tiffany3.jpg/100px-Tiffany3.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Chartreux",
"country" : "France",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/7/7f/Abbaye_fev2006_003.jpg/100px-Abbaye_fev2006_003.jpg"
}, {
"breed" : "Chausie",
"country" : "France",
"coffeePreference" : "Two Sugars",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/61/Chausiecatexample.jpg/100px-Chausiecatexample.jpg"
}, {
"breed" : "Cheetoh",
"country" : "United States",
"coffeePreference" : "Cream, No Sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/en/thumb/6/6a/Cheetoh_cat_chilling.png/100px-Cheetoh_cat_chilling.png"
}, {
"breed" : "Colorpoint Shorthair",
"country" : "",
"picture" : "https://upload.wikimedia.org/wikipedia/en/thumb/d/d5/Jake117.jpg/100px-Jake117.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Cornish Rex",
"country" : "United Kingdom (England)",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b8/BebopsLilacPrince.JPG/100px-BebopsLilacPrince.JPG"
}, {
"breed" : "Cymric or Manx Longhair",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e4/Cymric_-_Norwegia…ric_-_Norwegian_forest_cat_presentation_show_Kotka_2009-02-01_IMG_0687.JPG",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Cyprus",
"country" : "Cyprus",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b9/CyprusShorthair.jpg/100px-CyprusShorthair.jpg"
}, {
"breed" : "Devon Rex",
"country" : "United Kingdom (England)",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b0/Devon_Rex_Izzy.jpg/100px-Devon_Rex_Izzy.jpg"
}, {
"breed" : "Donskoy or Don Sphynx",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Cat_don_sphinx.JPG/100px-Cat_don_sphinx.JPG",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Dragon Li",
"country" : "China",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c2/Dragon_Li_-_Li_Hua_Mau1.jpg/100px-Dragon_Li_-_Li_Hua_Mau1.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Dwarf cat or Dwelf",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Egyptian Mau",
"country" : "Italy",
"coffeePreference" : "Cream, No Sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/Egy_mau.jpg/100px-Egy_mau.jpg"
}, {
"breed" : "European Shorthair",
"country" : "Sweden",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/European_shorthair_procumbent_Quincy.jpg/100px-European_shorthair_procumbent_Quincy.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Exotic Shorthair",
"country" : "United States",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Cream_tabby_exotic_cat.jpg/100px-Cream_tabby_exotic_cat.jpg"
}, {
"breed" : "German Rex",
"country" : "East Germany",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/German_Rex_Emi.jpg/100px-German_Rex_Emi.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Havana Brown",
"country" : "United Kingdom",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5d/Havana_Brown.jpg/100px-Havana_Brown.jpg"
}, {
"breed" : "Highlander",
"country" : "United Kingdom (Scotland)",
"coffeePreference" : "Americano"
}, {
"breed" : "Himalayan or Colorpoint Persian",
"coffeePreference" : "French Roast",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/d/dc/Chocolate_Himlayan.jpg/100px-Chocolate_Himlayan.jpg"
}, {
"breed" : "Japanese Bobtail",
"country" : "Japan",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/56/JapaneseBobtailBlueEyedMi-ke.JPG/100px-JapaneseBobtailBlueEyedMi-ke.JPG",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Javanese",
"country" : "United States",
"coffeePreference" : "French Roast",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6f/Javanese_cat.jpg/100px-Javanese_cat.jpg"
}, {
"breed" : "Kurilian Bobtail",
"country" : "Western Russia",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Khao Manee",
"country" : "Thailand",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/Khao_Manee_%22ChaWee%22.jpg/100px-Khao_Manee_%22ChaWee%22.jpg"
}, {
"breed" : "Korat",
"country" : "Thailand",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c1/Veda%2Cchat-adult…m%C3%A2le-race-korat.JPG/100px-Veda%2Cchat-adulte-m%C3%A2le-race-korat.JPG"
}, {
"breed" : "Korn Ja",
"country" : "Thailand",
"coffeePreference" : "Carmel Latte Grande"
}, {
"breed" : "Kurilian Bobtail or Kuril Islands Bobtail",
"country" : "Japan ",
"picture" : " https : //upload.wikimedia.org/wikipedia/commons/thumb/8/82/Kurilian_bobtail.JPG/100px-Kurilian_bobtail.JPG",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "LaPerm",
"country" : "United States",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ac/Laperm_LH_red_tabby.jpg/100px-Laperm_LH_red_tabby.jpg"
}, {
"breed" : "Lykoi",
"country" : "United States",
"coffeePreference" : "Two Sugars"
}, {
"breed" : "Maine Coon",
"country" : "United States",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/Maine_Coon_female.jpg/100px-Maine_Coon_female.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Manx",
"country" : "United Kingdom (Isle of Man)",
"coffeePreference" : "Skinny Mocha Latte",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e3/Japanese_Bobtail_looking_like_Manx.jpg/100px-Japanese_Bobtail_looking_like_Manx.jpg"
}, {
"breed" : "Mekong Bobtail",
"country" : "Russia",
"coffeePreference" : "French Roast",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Mekong_bobtail_fe…_Pride_cattery.jpg/100px-Mekong_bobtail_female%2C_Cofein_Pride_cattery.jpg"
}, {
"breed" : "Minskin",
"country" : "United States",
"coffeePreference" : "Americano",
"picture" : ""
}, {
"breed" : "Munchkin",
"country" : "United States",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Longhairedmunchkin.jpg/100px-Longhairedmunchkin.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Nebelung",
"country" : "United States",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/9e/Nebelung_Male%2C_…n_Song_de_Chine.JPG/100px-Nebelung_Male%2C_Aleksandr_van_Song_de_Chine.JPG"
}, {
"breed" : "Norwegian Forest Cat",
"country" : "Norway",
"coffeePreference" : "Skinny Mocha Latte",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Norskskogkatt_Evita_3.JPG/100px-Norskskogkatt_Evita_3.JPG"
}, {
"breed" : "Ocicat",
"country" : "United States",
"coffeePreference" : "Cream, No Sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/2/21/Ocicat-Charan.jpg/100px-Ocicat-Charan.jpg"
}, {
"breed" : "Oriental Bicolor",
"country" : "",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/6a/Oriental_shorthair_20070130_caroline.jpg/100px-Oriental_shorthair_20070130_caroline.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Oriental Shorthair",
"country" : "",
"coffeePreference" : "Skinny Mocha Latte",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/91/Oriental_Shorthai…ile%29.jpg/100px-Oriental_Shorthair_Blue_Eyed_White_cat_%28juvenile%29.jpg"
}, {
"breed" : "Oriental Longhair",
"country" : "",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/65/OLH-GIP_Divan_Cesar.jpg/100px-OLH-GIP_Divan_Cesar.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Persian (Modern Persian Cat)",
"country" : "United States",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Persialainen.jpg/100px-Persialainen.jpg"
}, {
"breed" : "Persian (Traditional Persian Cat)",
"country" : "Greater Iran",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/67/SnowyandHazy.jpg/100px-SnowyandHazy.jpg"
}, {
"breed" : "Peterbald",
"country" : "Russia",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b7/Tamila_the_lilac_tabby_Peterbald_cat.jpg/100px-Tamila_the_lilac_tabby_Peterbald_cat.jpg"
}, {
"breed" : "Pixie-bob",
"country" : "United States",
"coffeePreference" : "Cream, No Sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8c/Jarnac_Bepacific_feb07.jpg/100px-Jarnac_Bepacific_feb07.jpg"
}, {
"breed" : "Ragamuffin",
"country" : "United States",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/f/f3/20050922AmarilloRes.jpg/100px-20050922AmarilloRes.jpg"
}, {
"breed" : "Ragdoll",
"country" : "United States",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/Ragdoll_from_Gatil_Ragbelas.jpg/100px-Ragdoll_from_Gatil_Ragbelas.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Russian Blue",
"country" : "Russia",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/5f/Russian_Blue_001.gif/100px-Russian_Blue_001.gif"
}, {
"breed" : "Russian White, Black and Tabby",
"country" : "Australia",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Sam Sawet",
"country" : "Thailand",
"coffeePreference" : "Carmel Latte Grande"
}, {
"breed" : "Savannah",
"country" : "United States",
"coffeePreference" : "Cream, No Sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c4/Savannah_Cat_portrait.jpg/100px-Savannah_Cat_portrait.jpg"
}, {
"breed" : "Scottish Fold",
"country" : "United Kingdom (Scotland)",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Lilac_Scottish_Fold.jpg/100px-Lilac_Scottish_Fold.jpg"
}, {
"breed" : "Selkirk Rex",
"country" : "United States",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/en/thumb/c/c3/PolloSelkirkRex.jpg/100px-PolloSelkirkRex.jpg"
}, {
"breed" : "Serengeti",
"country" : "United States",
"coffeePreference" : "Cream, No Sugar",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1a/Serengetimalecat.jpg/100px-Serengetimalecat.jpg"
}, {
"breed" : "Serrade petit",
"country" : "France",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Serrade_petit.jpg/100px-Serrade_petit.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Siamese",
"country" : "Thailand",
"coffeePreference" : "French Roast",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/2/25/Siam_lilacpoint.jpg/100px-Siam_lilacpoint.jpg"
}, {
"breed" : "Siberian",
"country" : "Russia",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Qgcfillimor.jpg/100px-Qgcfillimor.jpg"
}, {
"breed" : "Singapura",
"country" : "Singapore",
"coffeePreference" : "Two Sugars",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/d/da/Raffles_singapura_cat.jpg/100px-Raffles_singapura_cat.jpg"
}, {
"breed" : "Snowshoe",
"country" : "United States",
"coffeePreference" : "French Roast",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/4/4b/Snowshoe_%28cat%29.JPG/100px-Snowshoe_%28cat%29.JPG"
}, {
"breed" : "Sokoke",
"country" : "Kenya",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/9/94/Sokoke_dalili.jpg/100px-Sokoke_dalili.jpg",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Somali",
"country" : "Somalia",
"coffeePreference" : "Two Sugars",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/c6/Blue_Somali_kitten_age_3_months.jpg/100px-Blue_Somali_kitten_age_3_months.jpg"
}, {
"breed" : "Sphynx",
"country" : "Canada",
"coffeePreference" : "Americano",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Sphinx2_July_2006.jpg/100px-Sphinx2_July_2006.jpg"
}, {
"breed" : "Suphalak",
"country" : "Thailand",
"coffeePreference" : "Carmel Latte Grande",
"picture" : "https://upload.wikimedia.org/wikipedia/en/thumb/5/52/Suphalak_Female_in_Tha…umDaengManee.jpg/100px-Suphalak_Female_in_Thailand_named_AumDaengManee.jpg"
}, {
"breed" : "Thai",
"country" : "Thailand",
"coffeePreference" : "French Roast",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/5/57/Mimbi3.JPG/100px-Mimbi3.JPG"
}, {
"breed" : "Tonkinese",
"country" : "Canada",
"coffeePreference" : "Turkish",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/8/81/Tonkinese.gif/100px-Tonkinese.gif"
}, {
"breed" : "Toyger",
"country" : "United States",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/c/ca/Toyger_-_Cornish_…ger_-_Cornish_Rex_presentation_show_Riihim%C3%A4ki_2008-11-16_IMG_0101.JPG",
"coffeePreference" : "Cafe Au Lait"
}, {
"breed" : "Turkish Angora",
"country" : "Turkey",
"coffeePreference" : "Skinny Mocha Latte",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/7/74/Angora.jpg/100px-Angora.jpg"
}, {
"breed" : "Turkish Van",
"country" : "developed in the United Kingdom (founding stock from Turkey)",
"picture" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/11/Turkish_Van_Example2.jpg/100px-Turkish_Van_Example2.jpg",
"coffeePreference" : "Cafe Au Lait"
}];
span {font-weight:bold}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox">descending<br>
<button>breed</button><button>country</button><button>coffeePreference</button>
<div id="out"></div>

Upvotes: 2

Related Questions