JSFiddle - React, Tailwind, and code Playground

by vikrant singh

JavaScript

var arr = [{
    "id": 1,
        "name": "Skateboard",
        "price": 1299,
        "currency": "SEK",
        "thumbnail": "/static/img/products/1-t.jpg"
},

{
    "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29000,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
},

{
    "id": 1,
        "name": "Skateboard",
        "price": 1299,
        "currency": "SEK",
        "thumbnail": "/static/img/products/1-t.jpg"
},

{
    "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29100,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
}, {
    "id": 4,
        "name": "A trip to the sun with Erik",
        "price": 29000,
        "currency": "SEK",
        "thumbnail": "/static/img/products/4-t.jpg"
}, ]

    function sort(arr) {
        if (arr.length == 0) return [];
        var result = [];
        var v = arr[0];
        result.push(v);
        arr.splice(0,1);;
        for (var i = 0; i < arr.length - 1; i++) {
            if (JSON.stringify(arr[i]) == JSON.stringify(v)) {
                result.push(arr[i]);
                arr.splice(i,1);
            }
        }
            return result.concat(sort(arr));
    }

console.log(JSON.stringify(sort(arr)));