JavaScript sortby the name by alphabet in array list using lodash

how to sort the name in array list by alphabet in javascript, I tried with this code

const sample = [
    {
        name: "AddMain",
        mesg: "test000"
    },
    {
        name: "Ballside",
        mesg: "test004545"
    },
    {
        name: "TestMain",
        mesg: "test00"
    },
    {
        name: "ball",
        mesg: "test004545"
    },
    {
        name: "Main",
        mesg: "test004545"
    },
    {
        name: "alliswell",
        mesg: "test004545"
    }
]

sample.sort(sortBy('name', false, function(a){return a.toUpperCase()}));

but it not working properly in this code sortBy I am using lodash. if it possible in lodash it will to fine

Answer:1

DEMO

const sample = [
    {
        name: "AddMain",
        mesg: "test000"
    },
    {
        name: "Ballside",
        mesg: "test004545"
    },
    {
        name: "TestMain",
        mesg: "test00"
    },
    {
        name: "ball",
        mesg: "test004545"
    },
    {
        name: "Main",
        mesg: "test004545"
    },
    {
        name: "alliswell",
        mesg: "test004545"
    }
];


var chars =_.orderBy(sample, [user => user.name.toLowerCase()], ['asc']);
 
console.log(chars);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
Answer:2

According to the lodash docs the API of sortBy is:

_.sortBy(collection, [iteratees=[_.identity]])

In your case, the collection is your sample array and your [iteratees=[_.identity]] should be a function or an array which returns the key you want to sort.

So this is probably what you were going for:

_.sortBy(sample, ['name']);

OR

_.sortBy(sample, function(o){return o.name;}]);

Answer:3

const sample = [
    {
        name: "AddMain",
        mesg: "test000"
    },
    {
        name: "Ballside",
        mesg: "test004545"
    },
    {
        name: "TestMain",
        mesg: "test00"
    },
    {
        name: "ball",
        mesg: "test004545"
    },
    {
        name: "Main",
        mesg: "test004545"
    },
    {
        name: "alliswell",
        mesg: "test004545"
    }
]

var result = sample.slice().sort((a,b) => a.name.localeCompare(b.name, undefined, {numeric: true}));
console.log(result);
Answer:4

I want left margin of every element in a class to have 70px left margin more than the last one. i.e. marginLeft += 70px; Here's what I have tried: function MarginLeftFunc() { var ...

I want left margin of every element in a class to have 70px left margin more than the last one. i.e. marginLeft += 70px; Here's what I have tried: function MarginLeftFunc() { var ...

How do I delete the created object "bird" from an array by name ie by the keyword "bird"? var storage = [ {cat: {name: "Garfild", count: 3443, price: 1000}} ]; function addProduct(...

How do I delete the created object "bird" from an array by name ie by the keyword "bird"? var storage = [ {cat: {name: "Garfild", count: 3443, price: 1000}} ]; function addProduct(...

If I create a inner class in a constructor, does it allocate memeory for each instance of the outer class that I create? For example, class PriorityQueue { constructor(maxSize) { // Set default ...

If I create a inner class in a constructor, does it allocate memeory for each instance of the outer class that I create? For example, class PriorityQueue { constructor(maxSize) { // Set default ...

I've come across this snippet and I have no idea why it does what it does. Try to answer the question yourself before looking at my answer below the code: what number will this alert? var foo = 1; ...

I've come across this snippet and I have no idea why it does what it does. Try to answer the question yourself before looking at my answer below the code: what number will this alert? var foo = 1; ...