Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

sort nested data using sort function javascript

var sort = function (prop, arr) {
    prop = prop.split('.');
    var len = prop.length;

    arr.sort(function (a, b) {
        var i = 0;
        while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
        if (a < b) {
            return -1;
        } else if (a > b) {
            return 1;
        } else {
            return 0;
        }
    });
    return arr;
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript random element from array 
Javascript :: recursion javascript 
Javascript :: detect livewire is loading in javascript 
Javascript :: how to change the first Letter to uppercase js 
Javascript :: how to make a game in unity with javascript 
Javascript :: connecting nodejs using mongoose 
Javascript :: moment get timezone 
Javascript :: js localstorage add text 
Javascript :: switch js 
Javascript :: javascript get multiple elements by id 
Javascript :: js array backwards 
Javascript :: if else js 
Javascript :: get child element of parent by class 
Javascript :: create node js api 
Javascript :: javascript object.assign 
Javascript :: find index in array javascript 
Javascript :: javascript function to open file browser 
Javascript :: .toJSON() in sequelize 
Javascript :: javascript remove space 
Javascript :: js scroll to id on body 
Javascript :: last index of javascript 
Javascript :: string to jspn js 
Javascript :: password confirmation using Joi 
Javascript :: how to display image in react js component 
Javascript :: react focus textarea 
Javascript :: javascript count no of lines 
Javascript :: setting timeout in javascript 
Javascript :: alpinejs with select 
Javascript :: window load 
Javascript :: javascript set class on div 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =