Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array in javascript with quotes [closed]

I have an array

var arr = ['hello','"end" two', 'end one', 'yes', 'abc' ];

I need to sort it as shown below

// abc,  end one, "end" two, hello, yes

What should i do?

like image 371
Sergey Andreev Avatar asked Dec 24 '22 15:12

Sergey Andreev


1 Answers

You could sort with a String#localeCompare's option.

ignorePunctuation

Whether punctuation should be ignored. Possible values are true and false; the default is false.

var array = ['hello', '"end" two', 'end one', 'yes', 'abc'];

array.sort(function (a, b) {
    return a.localeCompare(b, undefined, { ignorePunctuation: true });
});

console.log(array);
like image 134
Nina Scholz Avatar answered Dec 28 '22 09:12

Nina Scholz