2

After running a function i am getting an array like this.

    ["1:s", "2:2", "0:f"]

but i want to convert this array like this

     ["0:f","1:s","2:2"]

i mean index should be same as key.

2
  • Can you post some code of what you have tried? Commented Nov 2, 2016 at 8:52
  • please show your function Commented Nov 2, 2016 at 8:52

1 Answer 1

3

You could just sort it with taking the index out of the string.

var array = ["1:s", "2:2", "0:f"];
array.sort(function (a, b) {
    return a.split(':')[0] - b.split(':')[0];
});
console.log(array);

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

1 Comment

Note: first approach will be inconsistent as it sorts based on string. Try adding "12:g". Second approach is ideal

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.