Hello, does anybody know how can I sort words in string using javascript, jquery.
For example I have this:
var words = "1 3 2"
Now I want to reverse it to this:
var words = "2 3 1"
Thanks
Hello, does anybody know how can I sort words in string using javascript, jquery.
For example I have this:
var words = "1 3 2"
Now I want to reverse it to this:
var words = "2 3 1"
Thanks
Here's the basic idea, no need to import jQuery:
var words = "1 3 2"
var i=words.length;
i=i-1;
var reversedwords="";
for (var x = i; x >=0; x--)
{
reversedwords +=(words.charAt(x));
}
alert(reversedwords) // "2 3 1"
This would also work in reversing the string "string" to "gnirts"
Assuming you are reversing (I'm sure this'll still help if you're not).
var original = '1 3 2';
var reversed = original.split(' ').reverse().join(' ');