0

I have a string includig array symbol and double quote

var abc = '["Free WiFi","Breakfast for 2","Accommodation"]'

now i want this to convert into array and then sort this array.

the final result i want is Accomodation, Breakfast for 2, Free WiFi.

if array conversion and sorting is not require then also its fine.

how can we do it?

2 Answers 2

1

Just use JSON.parse and .sort:

var abc = JSON.parse('["Free WiFi","Breakfast for 2","Accommodation"]').sort().join(', ')
console.log(abc);

Sign up to request clarification or add additional context in comments.

2 Comments

it works but show like Accomodation,Breakfast for 2,Free Wifi. how can i add blank space every after comma? i want Accomodation, Breakfast for 2, Free Wifi.
probably just do .join(', ') at the end
0

Another option just for fun

var abc = '["Free WiFi","Breakfast for 2","Accommodation"]';

// With help of RegExp
match = (abc.replace(/(?:\[)*\"(.*?)\"(?:\])*/g, (m,g) => g)).split(",").sort().join(', ');

// Log
console.log(match)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.