-5

I would like to sort strings in javascript containing comma separated values in different e.g.

var Str = "8,0,2,10"

I want to sort it like below example form the last one to first one:

var NewStr = "10,2,0,8"
Ken Y-N
  • 14,644
  • 21
  • 71
  • 114
Raha
  • 15
  • 3
  • 1
    this is not string, not even array – sayalok Oct 07 '19 at 06:06
  • And it’s not sorting; it’s reversing. – Sebastian Simon Oct 07 '19 at 06:08
  • @Rajesh That’s really not a good dupe target. There’s a chance that the title will cause even more confusion (it’s impossible to change strings in-place in JS), and that question asks for reversing strings character by character, rather than splitting the string on commas, reversing that, then joining it again. – Sebastian Simon Oct 07 '19 at 06:14
  • I'm aware and i'm looking for more apt solution, however if you read properly, only thing missing is the use of delimiter. – Rajesh Oct 07 '19 at 06:15

1 Answers1

1

You can convert string to array using split() and reverse the array element using reverse() and then convert result to string again using join() like this:

var Str = '8,0,2,10';
var dif = Str.split(',').reverse().join(',');
console.log(dif);
Ankur Mishra
  • 1,284
  • 1
  • 6
  • 15